mirror of
https://github.com/Palm1r/QodeAssist.git
synced 2026-06-12 17:29:13 -04:00
refactor: Add external LLMCore lib (#334)
* feat: Add LLMCore submodule
This commit is contained in:
2
.github/workflows/build_cmake.yml
vendored
2
.github/workflows/build_cmake.yml
vendored
@@ -56,6 +56,8 @@ jobs:
|
|||||||
|
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@08eba0b27e820071cde6df949e0beb9ba4906955
|
- uses: actions/checkout@08eba0b27e820071cde6df949e0beb9ba4906955
|
||||||
|
with:
|
||||||
|
submodules: recursive
|
||||||
|
|
||||||
- name: Checkout submodules
|
- name: Checkout submodules
|
||||||
id: git
|
id: git
|
||||||
|
|||||||
3
.gitmodules
vendored
3
.gitmodules
vendored
@@ -0,0 +1,3 @@
|
|||||||
|
[submodule "sources/external/llmcore"]
|
||||||
|
path = sources/external/llmcore
|
||||||
|
url = https://github.com/Palm1r/llmcore.git
|
||||||
|
|||||||
@@ -34,7 +34,8 @@ add_definitions(
|
|||||||
-DQODEASSIST_QT_CREATOR_VERSION_PATCH=${QODEASSIST_QT_CREATOR_VERSION_PATCH}
|
-DQODEASSIST_QT_CREATOR_VERSION_PATCH=${QODEASSIST_QT_CREATOR_VERSION_PATCH}
|
||||||
)
|
)
|
||||||
|
|
||||||
add_subdirectory(llmcore)
|
add_subdirectory(sources/external/llmcore)
|
||||||
|
add_subdirectory(pluginllmcore)
|
||||||
add_subdirectory(settings)
|
add_subdirectory(settings)
|
||||||
add_subdirectory(logger)
|
add_subdirectory(logger)
|
||||||
add_subdirectory(UIControls)
|
add_subdirectory(UIControls)
|
||||||
@@ -61,6 +62,8 @@ add_qtc_plugin(QodeAssist
|
|||||||
QtCreator::ExtensionSystem
|
QtCreator::ExtensionSystem
|
||||||
QtCreator::Utils
|
QtCreator::Utils
|
||||||
QtCreator::CPlusPlus
|
QtCreator::CPlusPlus
|
||||||
|
LLMCore
|
||||||
|
PluginLLMCore
|
||||||
QodeAssistChatViewplugin
|
QodeAssistChatViewplugin
|
||||||
SOURCES
|
SOURCES
|
||||||
.github/workflows/build_cmake.yml
|
.github/workflows/build_cmake.yml
|
||||||
@@ -112,7 +115,6 @@ add_qtc_plugin(QodeAssist
|
|||||||
providers/OpenAIResponses/ItemTypesReference.hpp
|
providers/OpenAIResponses/ItemTypesReference.hpp
|
||||||
providers/OpenAIResponsesRequestBuilder.hpp
|
providers/OpenAIResponsesRequestBuilder.hpp
|
||||||
providers/OpenAIResponsesProvider.hpp providers/OpenAIResponsesProvider.cpp
|
providers/OpenAIResponsesProvider.hpp providers/OpenAIResponsesProvider.cpp
|
||||||
providers/OpenAIResponsesMessage.hpp providers/OpenAIResponsesMessage.cpp
|
|
||||||
QodeAssist.qrc
|
QodeAssist.qrc
|
||||||
LSPCompletion.hpp
|
LSPCompletion.hpp
|
||||||
LLMSuggestion.hpp LLMSuggestion.cpp
|
LLMSuggestion.hpp LLMSuggestion.cpp
|
||||||
@@ -141,10 +143,8 @@ add_qtc_plugin(QodeAssist
|
|||||||
widgets/DiffStatistics.hpp
|
widgets/DiffStatistics.hpp
|
||||||
|
|
||||||
QuickRefactorHandler.hpp QuickRefactorHandler.cpp
|
QuickRefactorHandler.hpp QuickRefactorHandler.cpp
|
||||||
tools/ToolsFactory.hpp tools/ToolsFactory.cpp
|
tools/ToolsRegistration.hpp tools/ToolsRegistration.cpp
|
||||||
tools/ToolHandler.hpp tools/ToolHandler.cpp
|
|
||||||
tools/ListProjectFilesTool.hpp tools/ListProjectFilesTool.cpp
|
tools/ListProjectFilesTool.hpp tools/ListProjectFilesTool.cpp
|
||||||
tools/ToolsManager.hpp tools/ToolsManager.cpp
|
|
||||||
tools/GetIssuesListTool.hpp tools/GetIssuesListTool.cpp
|
tools/GetIssuesListTool.hpp tools/GetIssuesListTool.cpp
|
||||||
tools/CreateNewFileTool.hpp tools/CreateNewFileTool.cpp
|
tools/CreateNewFileTool.hpp tools/CreateNewFileTool.cpp
|
||||||
tools/EditFileTool.hpp tools/EditFileTool.cpp
|
tools/EditFileTool.hpp tools/EditFileTool.cpp
|
||||||
@@ -154,10 +154,6 @@ add_qtc_plugin(QodeAssist
|
|||||||
tools/FindAndReadFileTool.hpp tools/FindAndReadFileTool.cpp
|
tools/FindAndReadFileTool.hpp tools/FindAndReadFileTool.cpp
|
||||||
tools/FileSearchUtils.hpp tools/FileSearchUtils.cpp
|
tools/FileSearchUtils.hpp tools/FileSearchUtils.cpp
|
||||||
tools/TodoTool.hpp tools/TodoTool.cpp
|
tools/TodoTool.hpp tools/TodoTool.cpp
|
||||||
providers/ClaudeMessage.hpp providers/ClaudeMessage.cpp
|
|
||||||
providers/OpenAIMessage.hpp providers/OpenAIMessage.cpp
|
|
||||||
providers/OllamaMessage.hpp providers/OllamaMessage.cpp
|
|
||||||
providers/GoogleMessage.hpp providers/GoogleMessage.cpp
|
|
||||||
)
|
)
|
||||||
|
|
||||||
get_target_property(QtCreatorCorePath QtCreator::Core LOCATION)
|
get_target_property(QtCreatorCorePath QtCreator::Core LOCATION)
|
||||||
|
|||||||
@@ -80,11 +80,12 @@ target_link_libraries(QodeAssistChatView
|
|||||||
Qt::Network
|
Qt::Network
|
||||||
QtCreator::Core
|
QtCreator::Core
|
||||||
QtCreator::Utils
|
QtCreator::Utils
|
||||||
LLMCore
|
PluginLLMCore
|
||||||
QodeAssistSettings
|
QodeAssistSettings
|
||||||
Context
|
Context
|
||||||
QodeAssistUIControlsplugin
|
QodeAssistUIControlsplugin
|
||||||
QodeAssistLogger
|
QodeAssistLogger
|
||||||
|
LLMCore
|
||||||
)
|
)
|
||||||
|
|
||||||
target_include_directories(QodeAssistChatView
|
target_include_directories(QodeAssistChatView
|
||||||
|
|||||||
@@ -18,6 +18,8 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "ChatCompressor.hpp"
|
#include "ChatCompressor.hpp"
|
||||||
|
|
||||||
|
#include <LLMCore/BaseClient.hpp>
|
||||||
#include "ChatModel.hpp"
|
#include "ChatModel.hpp"
|
||||||
#include "GeneralSettings.hpp"
|
#include "GeneralSettings.hpp"
|
||||||
#include "PromptTemplateManager.hpp"
|
#include "PromptTemplateManager.hpp"
|
||||||
@@ -56,7 +58,7 @@ void ChatCompressor::startCompression(const QString &chatFilePath, ChatModel *ch
|
|||||||
}
|
}
|
||||||
|
|
||||||
auto providerName = Settings::generalSettings().caProvider();
|
auto providerName = Settings::generalSettings().caProvider();
|
||||||
m_provider = LLMCore::ProvidersManager::instance().getProviderByName(providerName);
|
m_provider = PluginLLMCore::ProvidersManager::instance().getProviderByName(providerName);
|
||||||
|
|
||||||
if (!m_provider) {
|
if (!m_provider) {
|
||||||
emit compressionFailed(tr("No provider available"));
|
emit compressionFailed(tr("No provider available"));
|
||||||
@@ -64,7 +66,7 @@ void ChatCompressor::startCompression(const QString &chatFilePath, ChatModel *ch
|
|||||||
}
|
}
|
||||||
|
|
||||||
auto templateName = Settings::generalSettings().caTemplate();
|
auto templateName = Settings::generalSettings().caTemplate();
|
||||||
auto promptTemplate = LLMCore::PromptTemplateManager::instance().getChatTemplateByName(
|
auto promptTemplate = PluginLLMCore::PromptTemplateManager::instance().getChatTemplateByName(
|
||||||
templateName);
|
templateName);
|
||||||
|
|
||||||
if (!promptTemplate) {
|
if (!promptTemplate) {
|
||||||
@@ -76,7 +78,6 @@ void ChatCompressor::startCompression(const QString &chatFilePath, ChatModel *ch
|
|||||||
m_chatModel = chatModel;
|
m_chatModel = chatModel;
|
||||||
m_originalChatPath = chatFilePath;
|
m_originalChatPath = chatFilePath;
|
||||||
m_accumulatedSummary.clear();
|
m_accumulatedSummary.clear();
|
||||||
m_currentRequestId = QUuid::createUuid().toString(QUuid::WithoutBraces);
|
|
||||||
|
|
||||||
emit compressionStarted();
|
emit compressionStarted();
|
||||||
|
|
||||||
@@ -85,7 +86,7 @@ void ChatCompressor::startCompression(const QString &chatFilePath, ChatModel *ch
|
|||||||
QUrl requestUrl;
|
QUrl requestUrl;
|
||||||
QJsonObject payload;
|
QJsonObject payload;
|
||||||
|
|
||||||
if (m_provider->providerID() == LLMCore::ProviderID::GoogleAI) {
|
if (m_provider->providerID() == PluginLLMCore::ProviderID::GoogleAI) {
|
||||||
requestUrl = QUrl(QString("%1/models/%2:streamGenerateContent?alt=sse")
|
requestUrl = QUrl(QString("%1/models/%2:streamGenerateContent?alt=sse")
|
||||||
.arg(Settings::generalSettings().caUrl(),
|
.arg(Settings::generalSettings().caUrl(),
|
||||||
Settings::generalSettings().caModel()));
|
Settings::generalSettings().caModel()));
|
||||||
@@ -98,8 +99,8 @@ void ChatCompressor::startCompression(const QString &chatFilePath, ChatModel *ch
|
|||||||
|
|
||||||
buildRequestPayload(payload, promptTemplate);
|
buildRequestPayload(payload, promptTemplate);
|
||||||
|
|
||||||
|
m_currentRequestId = m_provider->sendRequest(requestUrl, payload);
|
||||||
LOG_MESSAGE(QString("Starting compression request: %1").arg(m_currentRequestId));
|
LOG_MESSAGE(QString("Starting compression request: %1").arg(m_currentRequestId));
|
||||||
m_provider->sendRequest(m_currentRequestId, requestUrl, payload);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ChatCompressor::isCompressing() const
|
bool ChatCompressor::isCompressing() const
|
||||||
@@ -188,28 +189,28 @@ QString ChatCompressor::buildCompressionPrompt() const
|
|||||||
}
|
}
|
||||||
|
|
||||||
void ChatCompressor::buildRequestPayload(
|
void ChatCompressor::buildRequestPayload(
|
||||||
QJsonObject &payload, LLMCore::PromptTemplate *promptTemplate)
|
QJsonObject &payload, PluginLLMCore::PromptTemplate *promptTemplate)
|
||||||
{
|
{
|
||||||
LLMCore::ContextData context;
|
PluginLLMCore::ContextData context;
|
||||||
|
|
||||||
context.systemPrompt = QStringLiteral(
|
context.systemPrompt = QStringLiteral(
|
||||||
"You are a helpful assistant that creates concise summaries of conversations. "
|
"You are a helpful assistant that creates concise summaries of conversations. "
|
||||||
"Your summaries preserve key information, technical details, and the flow of discussion.");
|
"Your summaries preserve key information, technical details, and the flow of discussion.");
|
||||||
|
|
||||||
QVector<LLMCore::Message> messages;
|
QVector<PluginLLMCore::Message> messages;
|
||||||
for (const auto &msg : m_chatModel->getChatHistory()) {
|
for (const auto &msg : m_chatModel->getChatHistory()) {
|
||||||
if (msg.role == ChatModel::ChatRole::Tool
|
if (msg.role == ChatModel::ChatRole::Tool
|
||||||
|| msg.role == ChatModel::ChatRole::FileEdit
|
|| msg.role == ChatModel::ChatRole::FileEdit
|
||||||
|| msg.role == ChatModel::ChatRole::Thinking)
|
|| msg.role == ChatModel::ChatRole::Thinking)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
LLMCore::Message apiMessage;
|
PluginLLMCore::Message apiMessage;
|
||||||
apiMessage.role = (msg.role == ChatModel::ChatRole::User) ? "user" : "assistant";
|
apiMessage.role = (msg.role == ChatModel::ChatRole::User) ? "user" : "assistant";
|
||||||
apiMessage.content = msg.content;
|
apiMessage.content = msg.content;
|
||||||
messages.append(apiMessage);
|
messages.append(apiMessage);
|
||||||
}
|
}
|
||||||
|
|
||||||
LLMCore::Message compressionRequest;
|
PluginLLMCore::Message compressionRequest;
|
||||||
compressionRequest.role = "user";
|
compressionRequest.role = "user";
|
||||||
compressionRequest.content = buildCompressionPrompt();
|
compressionRequest.content = buildCompressionPrompt();
|
||||||
messages.append(compressionRequest);
|
messages.append(compressionRequest);
|
||||||
@@ -217,7 +218,7 @@ void ChatCompressor::buildRequestPayload(
|
|||||||
context.history = messages;
|
context.history = messages;
|
||||||
|
|
||||||
m_provider->prepareRequest(
|
m_provider->prepareRequest(
|
||||||
payload, promptTemplate, context, LLMCore::RequestType::Chat, false, false);
|
payload, promptTemplate, context, PluginLLMCore::RequestType::Chat, false, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ChatCompressor::createCompressedChatFile(
|
bool ChatCompressor::createCompressedChatFile(
|
||||||
@@ -266,23 +267,25 @@ bool ChatCompressor::createCompressedChatFile(
|
|||||||
|
|
||||||
void ChatCompressor::connectProviderSignals()
|
void ChatCompressor::connectProviderSignals()
|
||||||
{
|
{
|
||||||
|
auto *c = m_provider->client();
|
||||||
|
|
||||||
m_connections.append(connect(
|
m_connections.append(connect(
|
||||||
m_provider,
|
c,
|
||||||
&LLMCore::Provider::partialResponseReceived,
|
&::LLMCore::BaseClient::chunkReceived,
|
||||||
this,
|
this,
|
||||||
&ChatCompressor::onPartialResponseReceived,
|
&ChatCompressor::onPartialResponseReceived,
|
||||||
Qt::UniqueConnection));
|
Qt::UniqueConnection));
|
||||||
|
|
||||||
m_connections.append(connect(
|
m_connections.append(connect(
|
||||||
m_provider,
|
c,
|
||||||
&LLMCore::Provider::fullResponseReceived,
|
&::LLMCore::BaseClient::requestCompleted,
|
||||||
this,
|
this,
|
||||||
&ChatCompressor::onFullResponseReceived,
|
&ChatCompressor::onFullResponseReceived,
|
||||||
Qt::UniqueConnection));
|
Qt::UniqueConnection));
|
||||||
|
|
||||||
m_connections.append(connect(
|
m_connections.append(connect(
|
||||||
m_provider,
|
c,
|
||||||
&LLMCore::Provider::requestFailed,
|
&::LLMCore::BaseClient::requestFailed,
|
||||||
this,
|
this,
|
||||||
&ChatCompressor::onRequestFailed,
|
&ChatCompressor::onRequestFailed,
|
||||||
Qt::UniqueConnection));
|
Qt::UniqueConnection));
|
||||||
|
|||||||
@@ -24,10 +24,10 @@
|
|||||||
#include <QObject>
|
#include <QObject>
|
||||||
#include <QString>
|
#include <QString>
|
||||||
|
|
||||||
namespace QodeAssist::LLMCore {
|
namespace QodeAssist::PluginLLMCore {
|
||||||
class Provider;
|
class Provider;
|
||||||
class PromptTemplate;
|
class PromptTemplate;
|
||||||
} // namespace QodeAssist::LLMCore
|
} // namespace QodeAssist::PluginLLMCore
|
||||||
|
|
||||||
namespace QodeAssist::Chat {
|
namespace QodeAssist::Chat {
|
||||||
|
|
||||||
@@ -64,13 +64,13 @@ private:
|
|||||||
void disconnectAllSignals();
|
void disconnectAllSignals();
|
||||||
void cleanupState();
|
void cleanupState();
|
||||||
void handleCompressionError(const QString &error);
|
void handleCompressionError(const QString &error);
|
||||||
void buildRequestPayload(QJsonObject &payload, LLMCore::PromptTemplate *promptTemplate);
|
void buildRequestPayload(QJsonObject &payload, PluginLLMCore::PromptTemplate *promptTemplate);
|
||||||
|
|
||||||
bool m_isCompressing = false;
|
bool m_isCompressing = false;
|
||||||
QString m_currentRequestId;
|
QString m_currentRequestId;
|
||||||
QString m_originalChatPath;
|
QString m_originalChatPath;
|
||||||
QString m_accumulatedSummary;
|
QString m_accumulatedSummary;
|
||||||
LLMCore::Provider *m_provider = nullptr;
|
PluginLLMCore::Provider *m_provider = nullptr;
|
||||||
ChatModel *m_chatModel = nullptr;
|
ChatModel *m_chatModel = nullptr;
|
||||||
|
|
||||||
QList<QMetaObject::Connection> m_connections;
|
QList<QMetaObject::Connection> m_connections;
|
||||||
|
|||||||
@@ -51,14 +51,14 @@
|
|||||||
#include "context/ChangesManager.h"
|
#include "context/ChangesManager.h"
|
||||||
#include "context/ContextManager.hpp"
|
#include "context/ContextManager.hpp"
|
||||||
#include "context/TokenUtils.hpp"
|
#include "context/TokenUtils.hpp"
|
||||||
#include "llmcore/RulesLoader.hpp"
|
#include "pluginllmcore/RulesLoader.hpp"
|
||||||
|
|
||||||
namespace QodeAssist::Chat {
|
namespace QodeAssist::Chat {
|
||||||
|
|
||||||
ChatRootView::ChatRootView(QQuickItem *parent)
|
ChatRootView::ChatRootView(QQuickItem *parent)
|
||||||
: QQuickItem(parent)
|
: QQuickItem(parent)
|
||||||
, m_chatModel(new ChatModel(this))
|
, m_chatModel(new ChatModel(this))
|
||||||
, m_promptProvider(LLMCore::PromptTemplateManager::instance())
|
, m_promptProvider(PluginLLMCore::PromptTemplateManager::instance())
|
||||||
, m_clientInterface(new ClientInterface(m_chatModel, &m_promptProvider, this))
|
, m_clientInterface(new ClientInterface(m_chatModel, &m_promptProvider, this))
|
||||||
, m_fileManager(new ChatFileManager(this))
|
, m_fileManager(new ChatFileManager(this))
|
||||||
, m_isRequestInProgress(false)
|
, m_isRequestInProgress(false)
|
||||||
@@ -929,7 +929,7 @@ QString ChatRootView::getRuleContent(int index)
|
|||||||
if (index < 0 || index >= m_activeRules.size())
|
if (index < 0 || index >= m_activeRules.size())
|
||||||
return QString();
|
return QString();
|
||||||
|
|
||||||
return LLMCore::RulesLoader::loadRuleFileContent(
|
return PluginLLMCore::RulesLoader::loadRuleFileContent(
|
||||||
m_activeRules[index].toMap()["filePath"].toString());
|
m_activeRules[index].toMap()["filePath"].toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -937,7 +937,7 @@ void ChatRootView::refreshRules()
|
|||||||
{
|
{
|
||||||
m_activeRules.clear();
|
m_activeRules.clear();
|
||||||
|
|
||||||
auto project = LLMCore::RulesLoader::getActiveProject();
|
auto project = PluginLLMCore::RulesLoader::getActiveProject();
|
||||||
if (!project) {
|
if (!project) {
|
||||||
emit activeRulesChanged();
|
emit activeRulesChanged();
|
||||||
emit activeRulesCountChanged();
|
emit activeRulesCountChanged();
|
||||||
@@ -945,7 +945,7 @@ void ChatRootView::refreshRules()
|
|||||||
}
|
}
|
||||||
|
|
||||||
auto ruleFiles
|
auto ruleFiles
|
||||||
= LLMCore::RulesLoader::getRuleFilesForProject(project, LLMCore::RulesContext::Chat);
|
= PluginLLMCore::RulesLoader::getRuleFilesForProject(project, PluginLLMCore::RulesContext::Chat);
|
||||||
|
|
||||||
for (const auto &ruleFile : ruleFiles) {
|
for (const auto &ruleFile : ruleFiles) {
|
||||||
QVariantMap ruleMap;
|
QVariantMap ruleMap;
|
||||||
@@ -1296,9 +1296,9 @@ QString ChatRootView::lastInfoMessage() const
|
|||||||
bool ChatRootView::isThinkingSupport() const
|
bool ChatRootView::isThinkingSupport() const
|
||||||
{
|
{
|
||||||
auto providerName = Settings::generalSettings().caProvider();
|
auto providerName = Settings::generalSettings().caProvider();
|
||||||
auto provider = LLMCore::ProvidersManager::instance().getProviderByName(providerName);
|
auto provider = PluginLLMCore::ProvidersManager::instance().getProviderByName(providerName);
|
||||||
|
|
||||||
return provider && provider->supportThinking();
|
return provider && provider->capabilities().testFlag(PluginLLMCore::ProviderCapability::Thinking);
|
||||||
}
|
}
|
||||||
|
|
||||||
QString ChatRootView::generateChatFileName(const QString &shortMessage, const QString &dir) const
|
QString ChatRootView::generateChatFileName(const QString &shortMessage, const QString &dir) const
|
||||||
|
|||||||
@@ -25,7 +25,7 @@
|
|||||||
#include "ChatFileManager.hpp"
|
#include "ChatFileManager.hpp"
|
||||||
#include "ChatModel.hpp"
|
#include "ChatModel.hpp"
|
||||||
#include "ClientInterface.hpp"
|
#include "ClientInterface.hpp"
|
||||||
#include "llmcore/PromptProviderChat.hpp"
|
#include "pluginllmcore/PromptProviderChat.hpp"
|
||||||
#include <coreplugin/editormanager/editormanager.h>
|
#include <coreplugin/editormanager/editormanager.h>
|
||||||
|
|
||||||
namespace QodeAssist::Chat {
|
namespace QodeAssist::Chat {
|
||||||
@@ -235,7 +235,7 @@ private:
|
|||||||
bool hasImageAttachments(const QStringList &attachments) const;
|
bool hasImageAttachments(const QStringList &attachments) const;
|
||||||
|
|
||||||
ChatModel *m_chatModel;
|
ChatModel *m_chatModel;
|
||||||
LLMCore::PromptProviderChat m_promptProvider;
|
PluginLLMCore::PromptProviderChat m_promptProvider;
|
||||||
ClientInterface *m_clientInterface;
|
ClientInterface *m_clientInterface;
|
||||||
ChatFileManager *m_fileManager;
|
ChatFileManager *m_fileManager;
|
||||||
QString m_currentTemplate;
|
QString m_currentTemplate;
|
||||||
|
|||||||
@@ -19,6 +19,8 @@
|
|||||||
|
|
||||||
#include "ClientInterface.hpp"
|
#include "ClientInterface.hpp"
|
||||||
|
|
||||||
|
#include <LLMCore/BaseClient.hpp>
|
||||||
|
|
||||||
#include <projectexplorer/buildconfiguration.h>
|
#include <projectexplorer/buildconfiguration.h>
|
||||||
#include <projectexplorer/target.h>
|
#include <projectexplorer/target.h>
|
||||||
#include <texteditor/textdocument.h>
|
#include <texteditor/textdocument.h>
|
||||||
@@ -40,6 +42,10 @@
|
|||||||
#include <texteditor/textdocument.h>
|
#include <texteditor/textdocument.h>
|
||||||
#include <texteditor/texteditor.h>
|
#include <texteditor/texteditor.h>
|
||||||
|
|
||||||
|
#include <LLMCore/ToolsManager.hpp>
|
||||||
|
|
||||||
|
#include "tools/TodoTool.hpp"
|
||||||
|
|
||||||
#include "ChatAssistantSettings.hpp"
|
#include "ChatAssistantSettings.hpp"
|
||||||
#include "ChatSerializer.hpp"
|
#include "ChatSerializer.hpp"
|
||||||
#include "GeneralSettings.hpp"
|
#include "GeneralSettings.hpp"
|
||||||
@@ -53,7 +59,7 @@
|
|||||||
namespace QodeAssist::Chat {
|
namespace QodeAssist::Chat {
|
||||||
|
|
||||||
ClientInterface::ClientInterface(
|
ClientInterface::ClientInterface(
|
||||||
ChatModel *chatModel, LLMCore::IPromptProvider *promptProvider, QObject *parent)
|
ChatModel *chatModel, PluginLLMCore::IPromptProvider *promptProvider, QObject *parent)
|
||||||
: QObject(parent)
|
: QObject(parent)
|
||||||
, m_chatModel(chatModel)
|
, m_chatModel(chatModel)
|
||||||
, m_promptProvider(promptProvider)
|
, m_promptProvider(promptProvider)
|
||||||
@@ -138,7 +144,7 @@ void ClientInterface::sendMessage(
|
|||||||
auto &chatAssistantSettings = Settings::chatAssistantSettings();
|
auto &chatAssistantSettings = Settings::chatAssistantSettings();
|
||||||
|
|
||||||
auto providerName = Settings::generalSettings().caProvider();
|
auto providerName = Settings::generalSettings().caProvider();
|
||||||
auto provider = LLMCore::ProvidersManager::instance().getProviderByName(providerName);
|
auto provider = PluginLLMCore::ProvidersManager::instance().getProviderByName(providerName);
|
||||||
|
|
||||||
if (!provider) {
|
if (!provider) {
|
||||||
LOG_MESSAGE(QString("No provider found with name: %1").arg(providerName));
|
LOG_MESSAGE(QString("No provider found with name: %1").arg(providerName));
|
||||||
@@ -153,7 +159,7 @@ void ClientInterface::sendMessage(
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
LLMCore::ContextData context;
|
PluginLLMCore::ContextData context;
|
||||||
|
|
||||||
const bool isToolsEnabled = useTools;
|
const bool isToolsEnabled = useTools;
|
||||||
|
|
||||||
@@ -167,7 +173,7 @@ void ClientInterface::sendMessage(
|
|||||||
systemPrompt = systemPrompt + "\n\n" + role.systemPrompt;
|
systemPrompt = systemPrompt + "\n\n" + role.systemPrompt;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto project = LLMCore::RulesLoader::getActiveProject();
|
auto project = PluginLLMCore::RulesLoader::getActiveProject();
|
||||||
|
|
||||||
if (project) {
|
if (project) {
|
||||||
systemPrompt += QString("\n# Active project name: %1").arg(project->displayName());
|
systemPrompt += QString("\n# Active project name: %1").arg(project->displayName());
|
||||||
@@ -177,12 +183,12 @@ void ClientInterface::sendMessage(
|
|||||||
if (auto target = project->activeTarget()) {
|
if (auto target = project->activeTarget()) {
|
||||||
if (auto buildConfig = target->activeBuildConfiguration()) {
|
if (auto buildConfig = target->activeBuildConfiguration()) {
|
||||||
systemPrompt += QString("\n# Active Build directory: %1")
|
systemPrompt += QString("\n# Active Build directory: %1")
|
||||||
.arg(buildConfig->buildDirectory().toUrlishString());
|
.arg(buildConfig->buildDirectory().toUrlishString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
QString projectRules
|
QString projectRules
|
||||||
= LLMCore::RulesLoader::loadRulesForProject(project, LLMCore::RulesContext::Chat);
|
= PluginLLMCore::RulesLoader::loadRulesForProject(project, PluginLLMCore::RulesContext::Chat);
|
||||||
|
|
||||||
if (!projectRules.isEmpty()) {
|
if (!projectRules.isEmpty()) {
|
||||||
systemPrompt += QString("\n# Project Rules\n\n") + projectRules;
|
systemPrompt += QString("\n# Project Rules\n\n") + projectRules;
|
||||||
@@ -197,13 +203,13 @@ void ClientInterface::sendMessage(
|
|||||||
context.systemPrompt = systemPrompt;
|
context.systemPrompt = systemPrompt;
|
||||||
}
|
}
|
||||||
|
|
||||||
QVector<LLMCore::Message> messages;
|
QVector<PluginLLMCore::Message> messages;
|
||||||
for (const auto &msg : m_chatModel->getChatHistory()) {
|
for (const auto &msg : m_chatModel->getChatHistory()) {
|
||||||
if (msg.role == ChatModel::ChatRole::Tool || msg.role == ChatModel::ChatRole::FileEdit) {
|
if (msg.role == ChatModel::ChatRole::Tool || msg.role == ChatModel::ChatRole::FileEdit) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
LLMCore::Message apiMessage;
|
PluginLLMCore::Message apiMessage;
|
||||||
apiMessage.role = msg.role == ChatModel::ChatRole::User ? "user" : "assistant";
|
apiMessage.role = msg.role == ChatModel::ChatRole::User ? "user" : "assistant";
|
||||||
apiMessage.content = msg.content;
|
apiMessage.content = msg.content;
|
||||||
|
|
||||||
@@ -223,7 +229,8 @@ void ClientInterface::sendMessage(
|
|||||||
apiMessage.isRedacted = msg.isRedacted;
|
apiMessage.isRedacted = msg.isRedacted;
|
||||||
apiMessage.signature = msg.signature;
|
apiMessage.signature = msg.signature;
|
||||||
|
|
||||||
if (provider->supportImage() && !m_chatFilePath.isEmpty() && !msg.images.isEmpty()) {
|
if (provider->capabilities().testFlag(PluginLLMCore::ProviderCapability::Image)
|
||||||
|
&& !m_chatFilePath.isEmpty() && !msg.images.isEmpty()) {
|
||||||
auto apiImages = loadImagesFromStorage(msg.images);
|
auto apiImages = loadImagesFromStorage(msg.images);
|
||||||
if (!apiImages.isEmpty()) {
|
if (!apiImages.isEmpty()) {
|
||||||
apiMessage.images = apiImages;
|
apiMessage.images = apiImages;
|
||||||
@@ -233,18 +240,19 @@ void ClientInterface::sendMessage(
|
|||||||
messages.append(apiMessage);
|
messages.append(apiMessage);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!imageFiles.isEmpty() && !provider->supportImage()) {
|
if (!imageFiles.isEmpty()
|
||||||
|
&& !provider->capabilities().testFlag(PluginLLMCore::ProviderCapability::Image)) {
|
||||||
LOG_MESSAGE(QString("Provider %1 doesn't support images, %2 ignored")
|
LOG_MESSAGE(QString("Provider %1 doesn't support images, %2 ignored")
|
||||||
.arg(provider->name(), QString::number(imageFiles.size())));
|
.arg(provider->name(), QString::number(imageFiles.size())));
|
||||||
}
|
}
|
||||||
|
|
||||||
context.history = messages;
|
context.history = messages;
|
||||||
|
|
||||||
LLMCore::LLMConfig config;
|
PluginLLMCore::LLMConfig config;
|
||||||
config.requestType = LLMCore::RequestType::Chat;
|
config.requestType = PluginLLMCore::RequestType::Chat;
|
||||||
config.provider = provider;
|
config.provider = provider;
|
||||||
config.promptTemplate = promptTemplate;
|
config.promptTemplate = promptTemplate;
|
||||||
if (provider->providerID() == LLMCore::ProviderID::GoogleAI) {
|
if (provider->providerID() == PluginLLMCore::ProviderID::GoogleAI) {
|
||||||
QString stream = QString{"streamGenerateContent?alt=sse"};
|
QString stream = QString{"streamGenerateContent?alt=sse"};
|
||||||
config.url = QUrl(QString("%1/models/%2:%3")
|
config.url = QUrl(QString("%1/models/%2:%3")
|
||||||
.arg(
|
.arg(
|
||||||
@@ -258,87 +266,79 @@ void ClientInterface::sendMessage(
|
|||||||
= {{"model", Settings::generalSettings().caModel()}, {"stream", true}};
|
= {{"model", Settings::generalSettings().caModel()}, {"stream", true}};
|
||||||
}
|
}
|
||||||
|
|
||||||
config.apiKey = provider->apiKey();
|
|
||||||
|
|
||||||
config.provider->prepareRequest(
|
config.provider->prepareRequest(
|
||||||
config.providerRequest,
|
config.providerRequest,
|
||||||
promptTemplate,
|
promptTemplate,
|
||||||
context,
|
context,
|
||||||
LLMCore::RequestType::Chat,
|
PluginLLMCore::RequestType::Chat,
|
||||||
useTools,
|
useTools,
|
||||||
useThinking);
|
useThinking);
|
||||||
|
|
||||||
QString requestId = QUuid::createUuid().toString();
|
connect(
|
||||||
|
provider->client(),
|
||||||
|
&::LLMCore::BaseClient::chunkReceived,
|
||||||
|
this,
|
||||||
|
&ClientInterface::handlePartialResponse,
|
||||||
|
Qt::UniqueConnection);
|
||||||
|
connect(
|
||||||
|
provider->client(),
|
||||||
|
&::LLMCore::BaseClient::requestCompleted,
|
||||||
|
this,
|
||||||
|
&ClientInterface::handleFullResponse,
|
||||||
|
Qt::UniqueConnection);
|
||||||
|
connect(
|
||||||
|
provider->client(),
|
||||||
|
&::LLMCore::BaseClient::requestFailed,
|
||||||
|
this,
|
||||||
|
&ClientInterface::handleRequestFailed,
|
||||||
|
Qt::UniqueConnection);
|
||||||
|
connect(
|
||||||
|
provider->client(),
|
||||||
|
&::LLMCore::BaseClient::toolStarted,
|
||||||
|
this,
|
||||||
|
&ClientInterface::handleToolExecutionStarted,
|
||||||
|
Qt::UniqueConnection);
|
||||||
|
connect(
|
||||||
|
provider->client(),
|
||||||
|
&::LLMCore::BaseClient::toolResultReady,
|
||||||
|
this,
|
||||||
|
&ClientInterface::handleToolExecutionCompleted,
|
||||||
|
Qt::UniqueConnection);
|
||||||
|
connect(
|
||||||
|
provider->client(),
|
||||||
|
&::LLMCore::BaseClient::thinkingBlockReceived,
|
||||||
|
this,
|
||||||
|
&ClientInterface::handleThinkingBlockReceived,
|
||||||
|
Qt::UniqueConnection);
|
||||||
|
|
||||||
|
auto requestId = provider->sendRequest(config.url, config.providerRequest);
|
||||||
QJsonObject request{{"id", requestId}};
|
QJsonObject request{{"id", requestId}};
|
||||||
|
|
||||||
m_activeRequests[requestId] = {request, provider};
|
m_activeRequests[requestId] = {request, provider};
|
||||||
|
|
||||||
emit requestStarted(requestId);
|
emit requestStarted(requestId);
|
||||||
|
|
||||||
connect(
|
if (provider->capabilities().testFlag(PluginLLMCore::ProviderCapability::Tools)
|
||||||
provider,
|
&& provider->toolsManager()) {
|
||||||
&LLMCore::Provider::partialResponseReceived,
|
if (auto *todoTool = qobject_cast<QodeAssist::Tools::TodoTool *>(
|
||||||
this,
|
provider->toolsManager()->tool("todo_tool"))) {
|
||||||
&ClientInterface::handlePartialResponse,
|
todoTool->setCurrentSessionId(m_chatFilePath);
|
||||||
Qt::UniqueConnection);
|
}
|
||||||
connect(
|
|
||||||
provider,
|
|
||||||
&LLMCore::Provider::fullResponseReceived,
|
|
||||||
this,
|
|
||||||
&ClientInterface::handleFullResponse,
|
|
||||||
Qt::UniqueConnection);
|
|
||||||
connect(
|
|
||||||
provider,
|
|
||||||
&LLMCore::Provider::requestFailed,
|
|
||||||
this,
|
|
||||||
&ClientInterface::handleRequestFailed,
|
|
||||||
Qt::UniqueConnection);
|
|
||||||
connect(
|
|
||||||
provider,
|
|
||||||
&LLMCore::Provider::toolExecutionStarted,
|
|
||||||
this,
|
|
||||||
&ClientInterface::handleToolExecutionStarted,
|
|
||||||
Qt::UniqueConnection);
|
|
||||||
connect(
|
|
||||||
provider,
|
|
||||||
&LLMCore::Provider::toolExecutionCompleted,
|
|
||||||
this,
|
|
||||||
&ClientInterface::handleToolExecutionCompleted,
|
|
||||||
Qt::UniqueConnection);
|
|
||||||
connect(
|
|
||||||
provider,
|
|
||||||
&LLMCore::Provider::continuationStarted,
|
|
||||||
this,
|
|
||||||
&ClientInterface::handleCleanAccumulatedData,
|
|
||||||
Qt::UniqueConnection);
|
|
||||||
connect(
|
|
||||||
provider,
|
|
||||||
&LLMCore::Provider::thinkingBlockReceived,
|
|
||||||
this,
|
|
||||||
&ClientInterface::handleThinkingBlockReceived,
|
|
||||||
Qt::UniqueConnection);
|
|
||||||
connect(
|
|
||||||
provider,
|
|
||||||
&LLMCore::Provider::redactedThinkingBlockReceived,
|
|
||||||
this,
|
|
||||||
&ClientInterface::handleRedactedThinkingBlockReceived,
|
|
||||||
Qt::UniqueConnection);
|
|
||||||
|
|
||||||
provider->sendRequest(requestId, config.url, config.providerRequest);
|
|
||||||
|
|
||||||
if (provider->supportsTools() && provider->toolsManager()) {
|
|
||||||
provider->toolsManager()->setCurrentSessionId(m_chatFilePath);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void ClientInterface::clearMessages()
|
void ClientInterface::clearMessages()
|
||||||
{
|
{
|
||||||
const auto providerName = Settings::generalSettings().caProvider();
|
const auto providerName = Settings::generalSettings().caProvider();
|
||||||
auto *provider = LLMCore::ProvidersManager::instance().getProviderByName(providerName);
|
auto *provider = PluginLLMCore::ProvidersManager::instance().getProviderByName(providerName);
|
||||||
|
|
||||||
if (provider && !m_chatFilePath.isEmpty() && provider->supportsTools()
|
if (provider && !m_chatFilePath.isEmpty()
|
||||||
|
&& provider->capabilities().testFlag(PluginLLMCore::ProviderCapability::Tools)
|
||||||
&& provider->toolsManager()) {
|
&& provider->toolsManager()) {
|
||||||
provider->toolsManager()->clearTodoSession(m_chatFilePath);
|
if (auto *todoTool = qobject_cast<QodeAssist::Tools::TodoTool *>(
|
||||||
|
provider->toolsManager()->tool("todo_tool"))) {
|
||||||
|
todoTool->clearSession(m_chatFilePath);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
m_chatModel->clear();
|
m_chatModel->clear();
|
||||||
@@ -346,7 +346,7 @@ void ClientInterface::clearMessages()
|
|||||||
|
|
||||||
void ClientInterface::cancelRequest()
|
void ClientInterface::cancelRequest()
|
||||||
{
|
{
|
||||||
QSet<LLMCore::Provider *> providers;
|
QSet<PluginLLMCore::Provider *> providers;
|
||||||
for (auto it = m_activeRequests.begin(); it != m_activeRequests.end(); ++it) {
|
for (auto it = m_activeRequests.begin(); it != m_activeRequests.end(); ++it) {
|
||||||
if (it.value().provider) {
|
if (it.value().provider) {
|
||||||
providers.insert(it.value().provider);
|
providers.insert(it.value().provider);
|
||||||
@@ -354,7 +354,7 @@ void ClientInterface::cancelRequest()
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (auto *provider : providers) {
|
for (auto *provider : providers) {
|
||||||
disconnect(provider, nullptr, this, nullptr);
|
disconnect(provider->client(), nullptr, this, nullptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (auto it = m_activeRequests.begin(); it != m_activeRequests.end(); ++it) {
|
for (auto it = m_activeRequests.begin(); it != m_activeRequests.end(); ++it) {
|
||||||
@@ -366,6 +366,7 @@ void ClientInterface::cancelRequest()
|
|||||||
|
|
||||||
m_activeRequests.clear();
|
m_activeRequests.clear();
|
||||||
m_accumulatedResponses.clear();
|
m_accumulatedResponses.clear();
|
||||||
|
m_awaitingContinuation.clear();
|
||||||
|
|
||||||
LOG_MESSAGE("All requests cancelled and state cleared");
|
LOG_MESSAGE("All requests cancelled and state cleared");
|
||||||
}
|
}
|
||||||
@@ -432,6 +433,12 @@ void ClientInterface::handlePartialResponse(const QString &requestId, const QStr
|
|||||||
if (it == m_activeRequests.end())
|
if (it == m_activeRequests.end())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
if (m_awaitingContinuation.remove(requestId)) {
|
||||||
|
m_accumulatedResponses[requestId].clear();
|
||||||
|
LOG_MESSAGE(
|
||||||
|
QString("Cleared accumulated responses for continuation request %1").arg(requestId));
|
||||||
|
}
|
||||||
|
|
||||||
m_accumulatedResponses[requestId] += partialText;
|
m_accumulatedResponses[requestId] += partialText;
|
||||||
|
|
||||||
const RequestContext &ctx = it.value();
|
const RequestContext &ctx = it.value();
|
||||||
@@ -462,12 +469,9 @@ void ClientInterface::handleFullResponse(const QString &requestId, const QString
|
|||||||
+ ": " + finalText);
|
+ ": " + finalText);
|
||||||
emit messageReceivedCompletely();
|
emit messageReceivedCompletely();
|
||||||
|
|
||||||
if (it != m_activeRequests.end()) {
|
m_activeRequests.erase(it);
|
||||||
m_activeRequests.erase(it);
|
m_accumulatedResponses.remove(requestId);
|
||||||
}
|
m_awaitingContinuation.remove(requestId);
|
||||||
if (m_accumulatedResponses.contains(requestId)) {
|
|
||||||
m_accumulatedResponses.remove(requestId);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ClientInterface::handleRequestFailed(const QString &requestId, const QString &error)
|
void ClientInterface::handleRequestFailed(const QString &requestId, const QString &error)
|
||||||
@@ -479,18 +483,9 @@ void ClientInterface::handleRequestFailed(const QString &requestId, const QStrin
|
|||||||
LOG_MESSAGE(QString("Chat request %1 failed: %2").arg(requestId, error));
|
LOG_MESSAGE(QString("Chat request %1 failed: %2").arg(requestId, error));
|
||||||
emit errorOccurred(error);
|
emit errorOccurred(error);
|
||||||
|
|
||||||
if (it != m_activeRequests.end()) {
|
m_activeRequests.erase(it);
|
||||||
m_activeRequests.erase(it);
|
m_accumulatedResponses.remove(requestId);
|
||||||
}
|
m_awaitingContinuation.remove(requestId);
|
||||||
if (m_accumulatedResponses.contains(requestId)) {
|
|
||||||
m_accumulatedResponses.remove(requestId);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void ClientInterface::handleCleanAccumulatedData(const QString &requestId)
|
|
||||||
{
|
|
||||||
m_accumulatedResponses[requestId].clear();
|
|
||||||
LOG_MESSAGE(QString("Cleared accumulated responses for continuation request %1").arg(requestId));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ClientInterface::handleThinkingBlockReceived(
|
void ClientInterface::handleThinkingBlockReceived(
|
||||||
@@ -501,19 +496,17 @@ void ClientInterface::handleThinkingBlockReceived(
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
m_chatModel->addThinkingBlock(requestId, thinking, signature);
|
if (m_awaitingContinuation.remove(requestId)) {
|
||||||
}
|
m_accumulatedResponses[requestId].clear();
|
||||||
|
|
||||||
void ClientInterface::handleRedactedThinkingBlockReceived(
|
|
||||||
const QString &requestId, const QString &signature)
|
|
||||||
{
|
|
||||||
if (!m_activeRequests.contains(requestId)) {
|
|
||||||
LOG_MESSAGE(
|
LOG_MESSAGE(
|
||||||
QString("Ignoring redacted thinking block for non-chat request: %1").arg(requestId));
|
QString("Cleared accumulated responses for continuation request %1").arg(requestId));
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
m_chatModel->addRedactedThinkingBlock(requestId, signature);
|
if (thinking.isEmpty()) {
|
||||||
|
m_chatModel->addRedactedThinkingBlock(requestId, signature);
|
||||||
|
} else {
|
||||||
|
m_chatModel->addThinkingBlock(requestId, thinking, signature);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void ClientInterface::handleToolExecutionStarted(
|
void ClientInterface::handleToolExecutionStarted(
|
||||||
@@ -525,6 +518,7 @@ void ClientInterface::handleToolExecutionStarted(
|
|||||||
}
|
}
|
||||||
|
|
||||||
m_chatModel->addToolExecutionStatus(requestId, toolId, toolName);
|
m_chatModel->addToolExecutionStatus(requestId, toolId, toolName);
|
||||||
|
m_awaitingContinuation.insert(requestId);
|
||||||
}
|
}
|
||||||
|
|
||||||
void ClientInterface::handleToolExecutionCompleted(
|
void ClientInterface::handleToolExecutionCompleted(
|
||||||
@@ -588,10 +582,10 @@ QString ClientInterface::encodeImageToBase64(const QString &filePath) const
|
|||||||
return imageData.toBase64();
|
return imageData.toBase64();
|
||||||
}
|
}
|
||||||
|
|
||||||
QVector<LLMCore::ImageAttachment> ClientInterface::loadImagesFromStorage(
|
QVector<PluginLLMCore::ImageAttachment> ClientInterface::loadImagesFromStorage(
|
||||||
const QList<ChatModel::ImageAttachment> &storedImages) const
|
const QList<ChatModel::ImageAttachment> &storedImages) const
|
||||||
{
|
{
|
||||||
QVector<LLMCore::ImageAttachment> apiImages;
|
QVector<PluginLLMCore::ImageAttachment> apiImages;
|
||||||
|
|
||||||
for (const auto &storedImage : storedImages) {
|
for (const auto &storedImage : storedImages) {
|
||||||
QString base64Data
|
QString base64Data
|
||||||
@@ -601,7 +595,7 @@ QVector<LLMCore::ImageAttachment> ClientInterface::loadImagesFromStorage(
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
LLMCore::ImageAttachment apiImage;
|
PluginLLMCore::ImageAttachment apiImage;
|
||||||
apiImage.data = base64Data;
|
apiImage.data = base64Data;
|
||||||
apiImage.mediaType = storedImage.mediaType;
|
apiImage.mediaType = storedImage.mediaType;
|
||||||
apiImage.isUrl = false;
|
apiImage.isUrl = false;
|
||||||
@@ -616,10 +610,15 @@ void ClientInterface::setChatFilePath(const QString &filePath)
|
|||||||
{
|
{
|
||||||
if (!m_chatFilePath.isEmpty() && m_chatFilePath != filePath) {
|
if (!m_chatFilePath.isEmpty() && m_chatFilePath != filePath) {
|
||||||
const auto providerName = Settings::generalSettings().caProvider();
|
const auto providerName = Settings::generalSettings().caProvider();
|
||||||
auto *provider = LLMCore::ProvidersManager::instance().getProviderByName(providerName);
|
auto *provider = PluginLLMCore::ProvidersManager::instance().getProviderByName(providerName);
|
||||||
|
|
||||||
if (provider && provider->supportsTools() && provider->toolsManager()) {
|
if (provider
|
||||||
provider->toolsManager()->clearTodoSession(m_chatFilePath);
|
&& provider->capabilities().testFlag(PluginLLMCore::ProviderCapability::Tools)
|
||||||
|
&& provider->toolsManager()) {
|
||||||
|
if (auto *todoTool = qobject_cast<QodeAssist::Tools::TodoTool *>(
|
||||||
|
provider->toolsManager()->tool("todo_tool"))) {
|
||||||
|
todoTool->clearSession(m_chatFilePath);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -20,12 +20,13 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <QObject>
|
#include <QObject>
|
||||||
|
#include <QSet>
|
||||||
#include <QString>
|
#include <QString>
|
||||||
#include <QVector>
|
#include <QVector>
|
||||||
|
|
||||||
#include "ChatModel.hpp"
|
#include "ChatModel.hpp"
|
||||||
#include "Provider.hpp"
|
#include "Provider.hpp"
|
||||||
#include "llmcore/IPromptProvider.hpp"
|
#include "pluginllmcore/IPromptProvider.hpp"
|
||||||
#include <context/ContextManager.hpp>
|
#include <context/ContextManager.hpp>
|
||||||
|
|
||||||
namespace QodeAssist::Chat {
|
namespace QodeAssist::Chat {
|
||||||
@@ -36,7 +37,7 @@ class ClientInterface : public QObject
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
explicit ClientInterface(
|
explicit ClientInterface(
|
||||||
ChatModel *chatModel, LLMCore::IPromptProvider *promptProvider, QObject *parent = nullptr);
|
ChatModel *chatModel, PluginLLMCore::IPromptProvider *promptProvider, QObject *parent = nullptr);
|
||||||
~ClientInterface();
|
~ClientInterface();
|
||||||
|
|
||||||
void sendMessage(
|
void sendMessage(
|
||||||
@@ -62,10 +63,8 @@ private slots:
|
|||||||
void handlePartialResponse(const QString &requestId, const QString &partialText);
|
void handlePartialResponse(const QString &requestId, const QString &partialText);
|
||||||
void handleFullResponse(const QString &requestId, const QString &fullText);
|
void handleFullResponse(const QString &requestId, const QString &fullText);
|
||||||
void handleRequestFailed(const QString &requestId, const QString &error);
|
void handleRequestFailed(const QString &requestId, const QString &error);
|
||||||
void handleCleanAccumulatedData(const QString &requestId);
|
|
||||||
void handleThinkingBlockReceived(
|
void handleThinkingBlockReceived(
|
||||||
const QString &requestId, const QString &thinking, const QString &signature);
|
const QString &requestId, const QString &thinking, const QString &signature);
|
||||||
void handleRedactedThinkingBlockReceived(const QString &requestId, const QString &signature);
|
|
||||||
void handleToolExecutionStarted(
|
void handleToolExecutionStarted(
|
||||||
const QString &requestId, const QString &toolId, const QString &toolName);
|
const QString &requestId, const QString &toolId, const QString &toolName);
|
||||||
void handleToolExecutionCompleted(
|
void handleToolExecutionCompleted(
|
||||||
@@ -82,21 +81,22 @@ private:
|
|||||||
bool isImageFile(const QString &filePath) const;
|
bool isImageFile(const QString &filePath) const;
|
||||||
QString getMediaTypeForImage(const QString &filePath) const;
|
QString getMediaTypeForImage(const QString &filePath) const;
|
||||||
QString encodeImageToBase64(const QString &filePath) const;
|
QString encodeImageToBase64(const QString &filePath) const;
|
||||||
QVector<LLMCore::ImageAttachment> loadImagesFromStorage(const QList<ChatModel::ImageAttachment> &storedImages) const;
|
QVector<PluginLLMCore::ImageAttachment> loadImagesFromStorage(const QList<ChatModel::ImageAttachment> &storedImages) const;
|
||||||
|
|
||||||
struct RequestContext
|
struct RequestContext
|
||||||
{
|
{
|
||||||
QJsonObject originalRequest;
|
QJsonObject originalRequest;
|
||||||
LLMCore::Provider *provider;
|
PluginLLMCore::Provider *provider;
|
||||||
};
|
};
|
||||||
|
|
||||||
LLMCore::IPromptProvider *m_promptProvider = nullptr;
|
PluginLLMCore::IPromptProvider *m_promptProvider = nullptr;
|
||||||
ChatModel *m_chatModel;
|
ChatModel *m_chatModel;
|
||||||
Context::ContextManager *m_contextManager;
|
Context::ContextManager *m_contextManager;
|
||||||
QString m_chatFilePath;
|
QString m_chatFilePath;
|
||||||
|
|
||||||
QHash<QString, RequestContext> m_activeRequests;
|
QHash<QString, RequestContext> m_activeRequests;
|
||||||
QHash<QString, QString> m_accumulatedResponses;
|
QHash<QString, QString> m_accumulatedResponses;
|
||||||
|
QSet<QString> m_awaitingContinuation;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace QodeAssist::Chat
|
} // namespace QodeAssist::Chat
|
||||||
|
|||||||
@@ -41,7 +41,7 @@ void ConfigurationManager::init()
|
|||||||
|
|
||||||
void ConfigurationManager::updateTemplateDescription(const Utils::StringAspect &templateAspect)
|
void ConfigurationManager::updateTemplateDescription(const Utils::StringAspect &templateAspect)
|
||||||
{
|
{
|
||||||
LLMCore::PromptTemplate *templ = m_templateManger.getFimTemplateByName(templateAspect.value());
|
PluginLLMCore::PromptTemplate *templ = m_templateManger.getFimTemplateByName(templateAspect.value());
|
||||||
|
|
||||||
if (!templ) {
|
if (!templ) {
|
||||||
return;
|
return;
|
||||||
@@ -65,7 +65,7 @@ void ConfigurationManager::updateAllTemplateDescriptions()
|
|||||||
|
|
||||||
void ConfigurationManager::checkTemplate(const Utils::StringAspect &templateAspect)
|
void ConfigurationManager::checkTemplate(const Utils::StringAspect &templateAspect)
|
||||||
{
|
{
|
||||||
LLMCore::PromptTemplate *templ = m_templateManger.getFimTemplateByName(templateAspect.value());
|
PluginLLMCore::PromptTemplate *templ = m_templateManger.getFimTemplateByName(templateAspect.value());
|
||||||
|
|
||||||
if (templ->name() == templateAspect.value())
|
if (templ->name() == templateAspect.value())
|
||||||
return;
|
return;
|
||||||
@@ -86,8 +86,8 @@ void ConfigurationManager::checkAllTemplate()
|
|||||||
ConfigurationManager::ConfigurationManager(QObject *parent)
|
ConfigurationManager::ConfigurationManager(QObject *parent)
|
||||||
: QObject(parent)
|
: QObject(parent)
|
||||||
, m_generalSettings(Settings::generalSettings())
|
, m_generalSettings(Settings::generalSettings())
|
||||||
, m_providersManager(LLMCore::ProvidersManager::instance())
|
, m_providersManager(PluginLLMCore::ProvidersManager::instance())
|
||||||
, m_templateManger(LLMCore::PromptTemplateManager::instance())
|
, m_templateManger(PluginLLMCore::PromptTemplateManager::instance())
|
||||||
{}
|
{}
|
||||||
|
|
||||||
void ConfigurationManager::setupConnections()
|
void ConfigurationManager::setupConnections()
|
||||||
@@ -176,7 +176,7 @@ void ConfigurationManager::selectModel()
|
|||||||
: m_generalSettings.caModel);
|
: m_generalSettings.caModel);
|
||||||
|
|
||||||
if (auto provider = m_providersManager.getProviderByName(providerName)) {
|
if (auto provider = m_providersManager.getProviderByName(providerName)) {
|
||||||
if (!provider->supportsModelListing()) {
|
if (!provider->capabilities().testFlag(PluginLLMCore::ProviderCapability::ModelListing)) {
|
||||||
m_generalSettings.showModelsNotSupportedDialog(*targetSettings);
|
m_generalSettings.showModelsNotSupportedDialog(*targetSettings);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,8 +21,8 @@
|
|||||||
|
|
||||||
#include <QObject>
|
#include <QObject>
|
||||||
|
|
||||||
#include "llmcore/PromptTemplateManager.hpp"
|
#include "pluginllmcore/PromptTemplateManager.hpp"
|
||||||
#include "llmcore/ProvidersManager.hpp"
|
#include "pluginllmcore/ProvidersManager.hpp"
|
||||||
#include "settings/GeneralSettings.hpp"
|
#include "settings/GeneralSettings.hpp"
|
||||||
|
|
||||||
namespace QodeAssist {
|
namespace QodeAssist {
|
||||||
@@ -54,8 +54,8 @@ private:
|
|||||||
ConfigurationManager &operator=(const ConfigurationManager &) = delete;
|
ConfigurationManager &operator=(const ConfigurationManager &) = delete;
|
||||||
|
|
||||||
Settings::GeneralSettings &m_generalSettings;
|
Settings::GeneralSettings &m_generalSettings;
|
||||||
LLMCore::ProvidersManager &m_providersManager;
|
PluginLLMCore::ProvidersManager &m_providersManager;
|
||||||
LLMCore::PromptTemplateManager &m_templateManger;
|
PluginLLMCore::PromptTemplateManager &m_templateManger;
|
||||||
|
|
||||||
void setupConnections();
|
void setupConnections();
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -19,6 +19,7 @@
|
|||||||
|
|
||||||
#include "LLMClientInterface.hpp"
|
#include "LLMClientInterface.hpp"
|
||||||
|
|
||||||
|
#include <LLMCore/BaseClient.hpp>
|
||||||
#include <QJsonDocument>
|
#include <QJsonDocument>
|
||||||
#include <QNetworkAccessManager>
|
#include <QNetworkAccessManager>
|
||||||
#include <QNetworkReply>
|
#include <QNetworkReply>
|
||||||
@@ -29,16 +30,16 @@
|
|||||||
#include "logger/Logger.hpp"
|
#include "logger/Logger.hpp"
|
||||||
#include "settings/CodeCompletionSettings.hpp"
|
#include "settings/CodeCompletionSettings.hpp"
|
||||||
#include "settings/GeneralSettings.hpp"
|
#include "settings/GeneralSettings.hpp"
|
||||||
#include <llmcore/RequestConfig.hpp>
|
#include <pluginllmcore/RequestConfig.hpp>
|
||||||
#include <llmcore/RulesLoader.hpp>
|
#include <pluginllmcore/RulesLoader.hpp>
|
||||||
|
|
||||||
namespace QodeAssist {
|
namespace QodeAssist {
|
||||||
|
|
||||||
LLMClientInterface::LLMClientInterface(
|
LLMClientInterface::LLMClientInterface(
|
||||||
const Settings::GeneralSettings &generalSettings,
|
const Settings::GeneralSettings &generalSettings,
|
||||||
const Settings::CodeCompletionSettings &completeSettings,
|
const Settings::CodeCompletionSettings &completeSettings,
|
||||||
LLMCore::IProviderRegistry &providerRegistry,
|
PluginLLMCore::IProviderRegistry &providerRegistry,
|
||||||
LLMCore::IPromptProvider *promptProvider,
|
PluginLLMCore::IPromptProvider *promptProvider,
|
||||||
Context::IDocumentReader &documentReader,
|
Context::IDocumentReader &documentReader,
|
||||||
IRequestPerformanceLogger &performanceLogger)
|
IRequestPerformanceLogger &performanceLogger)
|
||||||
: m_generalSettings(generalSettings)
|
: m_generalSettings(generalSettings)
|
||||||
@@ -122,8 +123,6 @@ void LLMClientInterface::sendData(const QByteArray &data)
|
|||||||
} else if (method == "textDocument/didOpen") {
|
} else if (method == "textDocument/didOpen") {
|
||||||
handleTextDocumentDidOpen(request);
|
handleTextDocumentDidOpen(request);
|
||||||
} else if (method == "getCompletionsCycling") {
|
} else if (method == "getCompletionsCycling") {
|
||||||
QString requestId = request["id"].toString();
|
|
||||||
m_performanceLogger.startTimeMeasurement(requestId);
|
|
||||||
handleCompletion(request);
|
handleCompletion(request);
|
||||||
} else if (method == "$/cancelRequest") {
|
} else if (method == "$/cancelRequest") {
|
||||||
handleCancelRequest();
|
handleCancelRequest();
|
||||||
@@ -136,7 +135,7 @@ void LLMClientInterface::sendData(const QByteArray &data)
|
|||||||
|
|
||||||
void LLMClientInterface::handleCancelRequest()
|
void LLMClientInterface::handleCancelRequest()
|
||||||
{
|
{
|
||||||
QSet<LLMCore::Provider *> providers;
|
QSet<PluginLLMCore::Provider *> providers;
|
||||||
for (auto it = m_activeRequests.begin(); it != m_activeRequests.end(); ++it) {
|
for (auto it = m_activeRequests.begin(); it != m_activeRequests.end(); ++it) {
|
||||||
if (it.value().provider) {
|
if (it.value().provider) {
|
||||||
providers.insert(it.value().provider);
|
providers.insert(it.value().provider);
|
||||||
@@ -144,7 +143,7 @@ void LLMClientInterface::handleCancelRequest()
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (auto *provider : providers) {
|
for (auto *provider : providers) {
|
||||||
disconnect(provider, nullptr, this, nullptr);
|
disconnect(provider->client(), nullptr, this, nullptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
for (auto it = m_activeRequests.begin(); it != m_activeRequests.end(); ++it) {
|
for (auto it = m_activeRequests.begin(); it != m_activeRequests.end(); ++it) {
|
||||||
@@ -271,12 +270,12 @@ void LLMClientInterface::handleCompletion(const QJsonObject &request)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// TODO refactor to dynamic presets system
|
// TODO refactor to dynamic presets system
|
||||||
LLMCore::LLMConfig config;
|
PluginLLMCore::LLMConfig config;
|
||||||
config.requestType = LLMCore::RequestType::CodeCompletion;
|
config.requestType = PluginLLMCore::RequestType::CodeCompletion;
|
||||||
config.provider = provider;
|
config.provider = provider;
|
||||||
config.promptTemplate = promptTemplate;
|
config.promptTemplate = promptTemplate;
|
||||||
// TODO refactor networking
|
// TODO refactor networking
|
||||||
if (provider->providerID() == LLMCore::ProviderID::GoogleAI) {
|
if (provider->providerID() == PluginLLMCore::ProviderID::GoogleAI) {
|
||||||
QString stream = QString{"streamGenerateContent?alt=sse"};
|
QString stream = QString{"streamGenerateContent?alt=sse"};
|
||||||
config.url = QUrl(QString("%1/models/%2:%3").arg(url, modelName, stream));
|
config.url = QUrl(QString("%1/models/%2:%3").arg(url, modelName, stream));
|
||||||
} else {
|
} else {
|
||||||
@@ -284,7 +283,6 @@ void LLMClientInterface::handleCompletion(const QJsonObject &request)
|
|||||||
QString("%1%2").arg(url, endpoint(provider, promptTemplate->type(), isPreset1Active)));
|
QString("%1%2").arg(url, endpoint(provider, promptTemplate->type(), isPreset1Active)));
|
||||||
config.providerRequest = {{"model", modelName}, {"stream", true}};
|
config.providerRequest = {{"model", modelName}, {"stream", true}};
|
||||||
}
|
}
|
||||||
config.apiKey = provider->apiKey();
|
|
||||||
config.multiLineCompletion = m_completeSettings.multiLineCompletion();
|
config.multiLineCompletion = m_completeSettings.multiLineCompletion();
|
||||||
|
|
||||||
const auto stopWords = QJsonArray::fromStringList(config.promptTemplate->stopWords());
|
const auto stopWords = QJsonArray::fromStringList(config.promptTemplate->stopWords());
|
||||||
@@ -295,14 +293,14 @@ void LLMClientInterface::handleCompletion(const QJsonObject &request)
|
|||||||
if (m_completeSettings.useSystemPrompt())
|
if (m_completeSettings.useSystemPrompt())
|
||||||
systemPrompt.append(
|
systemPrompt.append(
|
||||||
m_completeSettings.useUserMessageTemplateForCC()
|
m_completeSettings.useUserMessageTemplateForCC()
|
||||||
&& promptTemplate->type() == LLMCore::TemplateType::Chat
|
&& promptTemplate->type() == PluginLLMCore::TemplateType::Chat
|
||||||
? m_completeSettings.systemPromptForNonFimModels()
|
? m_completeSettings.systemPromptForNonFimModels()
|
||||||
: m_completeSettings.systemPrompt());
|
: m_completeSettings.systemPrompt());
|
||||||
|
|
||||||
auto project = LLMCore::RulesLoader::getActiveProject();
|
auto project = PluginLLMCore::RulesLoader::getActiveProject();
|
||||||
if (project) {
|
if (project) {
|
||||||
QString projectRules
|
QString projectRules
|
||||||
= LLMCore::RulesLoader::loadRulesForProject(project, LLMCore::RulesContext::Completions);
|
= PluginLLMCore::RulesLoader::loadRulesForProject(project, PluginLLMCore::RulesContext::Completions);
|
||||||
|
|
||||||
if (!projectRules.isEmpty()) {
|
if (!projectRules.isEmpty()) {
|
||||||
systemPrompt += "\n\n# Project Rules\n\n" + projectRules;
|
systemPrompt += "\n\n# Project Rules\n\n" + projectRules;
|
||||||
@@ -314,10 +312,10 @@ void LLMClientInterface::handleCompletion(const QJsonObject &request)
|
|||||||
systemPrompt.append(updatedContext.fileContext.value());
|
systemPrompt.append(updatedContext.fileContext.value());
|
||||||
|
|
||||||
if (m_completeSettings.useOpenFilesContext()) {
|
if (m_completeSettings.useOpenFilesContext()) {
|
||||||
if (provider->providerID() == LLMCore::ProviderID::LlamaCpp) {
|
if (provider->providerID() == PluginLLMCore::ProviderID::LlamaCpp) {
|
||||||
for (const auto openedFilePath : m_contextManager->openedFiles({filePath})) {
|
for (const auto openedFilePath : m_contextManager->openedFiles({filePath})) {
|
||||||
if (!updatedContext.filesMetadata) {
|
if (!updatedContext.filesMetadata) {
|
||||||
updatedContext.filesMetadata = QList<LLMCore::FileMetadata>();
|
updatedContext.filesMetadata = QList<PluginLLMCore::FileMetadata>();
|
||||||
}
|
}
|
||||||
updatedContext.filesMetadata->append({openedFilePath.first, openedFilePath.second});
|
updatedContext.filesMetadata->append({openedFilePath.first, openedFilePath.second});
|
||||||
}
|
}
|
||||||
@@ -328,7 +326,7 @@ void LLMClientInterface::handleCompletion(const QJsonObject &request)
|
|||||||
|
|
||||||
updatedContext.systemPrompt = systemPrompt;
|
updatedContext.systemPrompt = systemPrompt;
|
||||||
|
|
||||||
if (promptTemplate->type() == LLMCore::TemplateType::Chat) {
|
if (promptTemplate->type() == PluginLLMCore::TemplateType::Chat) {
|
||||||
QString userMessage;
|
QString userMessage;
|
||||||
if (m_completeSettings.useUserMessageTemplateForCC()) {
|
if (m_completeSettings.useUserMessageTemplateForCC()) {
|
||||||
userMessage = m_completeSettings.processMessageToFIM(
|
userMessage = m_completeSettings.processMessageToFIM(
|
||||||
@@ -338,7 +336,7 @@ void LLMClientInterface::handleCompletion(const QJsonObject &request)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// TODO refactor add message
|
// TODO refactor add message
|
||||||
QVector<LLMCore::Message> messages;
|
QVector<PluginLLMCore::Message> messages;
|
||||||
messages.append({"user", userMessage});
|
messages.append({"user", userMessage});
|
||||||
updatedContext.history = messages;
|
updatedContext.history = messages;
|
||||||
}
|
}
|
||||||
@@ -347,41 +345,29 @@ void LLMClientInterface::handleCompletion(const QJsonObject &request)
|
|||||||
config.providerRequest,
|
config.providerRequest,
|
||||||
promptTemplate,
|
promptTemplate,
|
||||||
updatedContext,
|
updatedContext,
|
||||||
LLMCore::RequestType::CodeCompletion,
|
PluginLLMCore::RequestType::CodeCompletion,
|
||||||
false,
|
false,
|
||||||
false);
|
false);
|
||||||
|
|
||||||
auto errors = config.provider->validateRequest(config.providerRequest, promptTemplate->type());
|
|
||||||
if (!errors.isEmpty()) {
|
|
||||||
QString error = QString("Request validation failed: %1").arg(errors.join("; "));
|
|
||||||
LOG_MESSAGE("Validate errors for request:");
|
|
||||||
LOG_MESSAGES(errors);
|
|
||||||
sendErrorResponse(request, error);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
QString requestId = request["id"].toString();
|
|
||||||
m_performanceLogger.startTimeMeasurement(requestId);
|
|
||||||
|
|
||||||
m_activeRequests[requestId] = {request, provider};
|
|
||||||
|
|
||||||
connect(
|
connect(
|
||||||
provider,
|
provider->client(),
|
||||||
&LLMCore::Provider::fullResponseReceived,
|
&::LLMCore::BaseClient::requestCompleted,
|
||||||
this,
|
this,
|
||||||
&LLMClientInterface::handleFullResponse,
|
&LLMClientInterface::handleFullResponse,
|
||||||
Qt::UniqueConnection);
|
Qt::UniqueConnection);
|
||||||
connect(
|
connect(
|
||||||
provider,
|
provider->client(),
|
||||||
&LLMCore::Provider::requestFailed,
|
&::LLMCore::BaseClient::requestFailed,
|
||||||
this,
|
this,
|
||||||
&LLMClientInterface::handleRequestFailed,
|
&LLMClientInterface::handleRequestFailed,
|
||||||
Qt::UniqueConnection);
|
Qt::UniqueConnection);
|
||||||
|
|
||||||
provider->sendRequest(requestId, config.url, config.providerRequest);
|
auto requestId = provider->sendRequest(config.url, config.providerRequest);
|
||||||
|
m_activeRequests[requestId] = {request, provider};
|
||||||
|
m_performanceLogger.startTimeMeasurement(requestId);
|
||||||
}
|
}
|
||||||
|
|
||||||
LLMCore::ContextData LLMClientInterface::prepareContext(
|
PluginLLMCore::ContextData LLMClientInterface::prepareContext(
|
||||||
const QJsonObject &request, const Context::DocumentInfo &documentInfo)
|
const QJsonObject &request, const Context::DocumentInfo &documentInfo)
|
||||||
{
|
{
|
||||||
QJsonObject params = request["params"].toObject();
|
QJsonObject params = request["params"].toObject();
|
||||||
@@ -396,13 +382,13 @@ LLMCore::ContextData LLMClientInterface::prepareContext(
|
|||||||
}
|
}
|
||||||
|
|
||||||
QString LLMClientInterface::endpoint(
|
QString LLMClientInterface::endpoint(
|
||||||
LLMCore::Provider *provider, LLMCore::TemplateType type, bool isLanguageSpecify)
|
PluginLLMCore::Provider *provider, PluginLLMCore::TemplateType type, bool isLanguageSpecify)
|
||||||
{
|
{
|
||||||
QString endpoint;
|
QString endpoint;
|
||||||
auto endpointMode = isLanguageSpecify ? m_generalSettings.ccPreset1EndpointMode.stringValue()
|
auto endpointMode = isLanguageSpecify ? m_generalSettings.ccPreset1EndpointMode.stringValue()
|
||||||
: m_generalSettings.ccEndpointMode.stringValue();
|
: m_generalSettings.ccEndpointMode.stringValue();
|
||||||
if (endpointMode == "Auto") {
|
if (endpointMode == "Auto") {
|
||||||
endpoint = type == LLMCore::TemplateType::FIM ? provider->completionEndpoint()
|
endpoint = type == PluginLLMCore::TemplateType::FIM ? provider->completionEndpoint()
|
||||||
: provider->chatEndpoint();
|
: provider->chatEndpoint();
|
||||||
} else if (endpointMode == "Custom") {
|
} else if (endpointMode == "Custom") {
|
||||||
endpoint = isLanguageSpecify ? m_generalSettings.ccPreset1CustomEndpoint()
|
endpoint = isLanguageSpecify ? m_generalSettings.ccPreset1CustomEndpoint()
|
||||||
|
|||||||
@@ -25,9 +25,9 @@
|
|||||||
#include <context/ContextManager.hpp>
|
#include <context/ContextManager.hpp>
|
||||||
#include <context/IDocumentReader.hpp>
|
#include <context/IDocumentReader.hpp>
|
||||||
#include <context/ProgrammingLanguage.hpp>
|
#include <context/ProgrammingLanguage.hpp>
|
||||||
#include <llmcore/ContextData.hpp>
|
#include <pluginllmcore/ContextData.hpp>
|
||||||
#include <llmcore/IPromptProvider.hpp>
|
#include <pluginllmcore/IPromptProvider.hpp>
|
||||||
#include <llmcore/IProviderRegistry.hpp>
|
#include <pluginllmcore/IProviderRegistry.hpp>
|
||||||
#include <logger/IRequestPerformanceLogger.hpp>
|
#include <logger/IRequestPerformanceLogger.hpp>
|
||||||
#include <settings/CodeCompletionSettings.hpp>
|
#include <settings/CodeCompletionSettings.hpp>
|
||||||
#include <settings/GeneralSettings.hpp>
|
#include <settings/GeneralSettings.hpp>
|
||||||
@@ -45,8 +45,8 @@ public:
|
|||||||
LLMClientInterface(
|
LLMClientInterface(
|
||||||
const Settings::GeneralSettings &generalSettings,
|
const Settings::GeneralSettings &generalSettings,
|
||||||
const Settings::CodeCompletionSettings &completeSettings,
|
const Settings::CodeCompletionSettings &completeSettings,
|
||||||
LLMCore::IProviderRegistry &providerRegistry,
|
PluginLLMCore::IProviderRegistry &providerRegistry,
|
||||||
LLMCore::IPromptProvider *promptProvider,
|
PluginLLMCore::IPromptProvider *promptProvider,
|
||||||
Context::IDocumentReader &documentReader,
|
Context::IDocumentReader &documentReader,
|
||||||
IRequestPerformanceLogger &performanceLogger);
|
IRequestPerformanceLogger &performanceLogger);
|
||||||
~LLMClientInterface() override;
|
~LLMClientInterface() override;
|
||||||
@@ -82,17 +82,17 @@ private:
|
|||||||
struct RequestContext
|
struct RequestContext
|
||||||
{
|
{
|
||||||
QJsonObject originalRequest;
|
QJsonObject originalRequest;
|
||||||
LLMCore::Provider *provider;
|
PluginLLMCore::Provider *provider;
|
||||||
};
|
};
|
||||||
|
|
||||||
LLMCore::ContextData prepareContext(
|
PluginLLMCore::ContextData prepareContext(
|
||||||
const QJsonObject &request, const Context::DocumentInfo &documentInfo);
|
const QJsonObject &request, const Context::DocumentInfo &documentInfo);
|
||||||
QString endpoint(LLMCore::Provider *provider, LLMCore::TemplateType type, bool isLanguageSpecify);
|
QString endpoint(PluginLLMCore::Provider *provider, PluginLLMCore::TemplateType type, bool isLanguageSpecify);
|
||||||
|
|
||||||
const Settings::CodeCompletionSettings &m_completeSettings;
|
const Settings::CodeCompletionSettings &m_completeSettings;
|
||||||
const Settings::GeneralSettings &m_generalSettings;
|
const Settings::GeneralSettings &m_generalSettings;
|
||||||
LLMCore::IPromptProvider *m_promptProvider = nullptr;
|
PluginLLMCore::IPromptProvider *m_promptProvider = nullptr;
|
||||||
LLMCore::IProviderRegistry &m_providerRegistry;
|
PluginLLMCore::IProviderRegistry &m_providerRegistry;
|
||||||
Context::IDocumentReader &m_documentReader;
|
Context::IDocumentReader &m_documentReader;
|
||||||
IRequestPerformanceLogger &m_performanceLogger;
|
IRequestPerformanceLogger &m_performanceLogger;
|
||||||
QElapsedTimer m_completionTimer;
|
QElapsedTimer m_completionTimer;
|
||||||
|
|||||||
@@ -36,8 +36,8 @@
|
|||||||
#include "widgets/EditorChatButtonHandler.hpp"
|
#include "widgets/EditorChatButtonHandler.hpp"
|
||||||
#include "widgets/RefactorWidgetHandler.hpp"
|
#include "widgets/RefactorWidgetHandler.hpp"
|
||||||
#include <languageclient/client.h>
|
#include <languageclient/client.h>
|
||||||
#include <llmcore/IPromptProvider.hpp>
|
#include <pluginllmcore/IPromptProvider.hpp>
|
||||||
#include <llmcore/IProviderRegistry.hpp>
|
#include <pluginllmcore/IProviderRegistry.hpp>
|
||||||
|
|
||||||
namespace QodeAssist {
|
namespace QodeAssist {
|
||||||
|
|
||||||
|
|||||||
@@ -19,18 +19,19 @@
|
|||||||
|
|
||||||
#include "QuickRefactorHandler.hpp"
|
#include "QuickRefactorHandler.hpp"
|
||||||
|
|
||||||
|
#include <LLMCore/BaseClient.hpp>
|
||||||
#include <QJsonArray>
|
#include <QJsonArray>
|
||||||
#include <QJsonDocument>
|
#include <QJsonDocument>
|
||||||
#include <QUuid>
|
#include <QUuid>
|
||||||
|
|
||||||
#include <context/DocumentContextReader.hpp>
|
#include <context/DocumentContextReader.hpp>
|
||||||
#include <llmcore/ResponseCleaner.hpp>
|
#include <pluginllmcore/ResponseCleaner.hpp>
|
||||||
#include <context/DocumentReaderQtCreator.hpp>
|
#include <context/DocumentReaderQtCreator.hpp>
|
||||||
#include <context/Utils.hpp>
|
#include <context/Utils.hpp>
|
||||||
#include <llmcore/PromptTemplateManager.hpp>
|
#include <pluginllmcore/PromptTemplateManager.hpp>
|
||||||
#include <llmcore/ProvidersManager.hpp>
|
#include <pluginllmcore/ProvidersManager.hpp>
|
||||||
#include <llmcore/RequestConfig.hpp>
|
#include <pluginllmcore/RequestConfig.hpp>
|
||||||
#include <llmcore/RulesLoader.hpp>
|
#include <pluginllmcore/RulesLoader.hpp>
|
||||||
#include <logger/Logger.hpp>
|
#include <logger/Logger.hpp>
|
||||||
#include <settings/ChatAssistantSettings.hpp>
|
#include <settings/ChatAssistantSettings.hpp>
|
||||||
#include <settings/GeneralSettings.hpp>
|
#include <settings/GeneralSettings.hpp>
|
||||||
@@ -109,8 +110,8 @@ void QuickRefactorHandler::prepareAndSendRequest(
|
|||||||
{
|
{
|
||||||
auto &settings = Settings::generalSettings();
|
auto &settings = Settings::generalSettings();
|
||||||
|
|
||||||
auto &providerRegistry = LLMCore::ProvidersManager::instance();
|
auto &providerRegistry = PluginLLMCore::ProvidersManager::instance();
|
||||||
auto &promptManager = LLMCore::PromptTemplateManager::instance();
|
auto &promptManager = PluginLLMCore::PromptTemplateManager::instance();
|
||||||
|
|
||||||
const auto providerName = settings.qrProvider();
|
const auto providerName = settings.qrProvider();
|
||||||
auto provider = providerRegistry.getProviderByName(providerName);
|
auto provider = providerRegistry.getProviderByName(providerName);
|
||||||
@@ -140,14 +141,13 @@ void QuickRefactorHandler::prepareAndSendRequest(
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
LLMCore::LLMConfig config;
|
PluginLLMCore::LLMConfig config;
|
||||||
config.requestType = LLMCore::RequestType::QuickRefactoring;
|
config.requestType = PluginLLMCore::RequestType::QuickRefactoring;
|
||||||
config.provider = provider;
|
config.provider = provider;
|
||||||
config.promptTemplate = promptTemplate;
|
config.promptTemplate = promptTemplate;
|
||||||
config.url = QString("%1%2").arg(settings.qrUrl(), provider->chatEndpoint());
|
config.url = QString("%1%2").arg(settings.qrUrl(), provider->chatEndpoint());
|
||||||
config.apiKey = provider->apiKey();
|
|
||||||
|
|
||||||
if (provider->providerID() == LLMCore::ProviderID::GoogleAI) {
|
if (provider->providerID() == PluginLLMCore::ProviderID::GoogleAI) {
|
||||||
QString stream = QString{"streamGenerateContent?alt=sse"};
|
QString stream = QString{"streamGenerateContent?alt=sse"};
|
||||||
config.url = QUrl(QString("%1/models/%2:%3")
|
config.url = QUrl(QString("%1/models/%2:%3")
|
||||||
.arg(
|
.arg(
|
||||||
@@ -161,7 +161,7 @@ void QuickRefactorHandler::prepareAndSendRequest(
|
|||||||
= {{"model", Settings::generalSettings().qrModel()}, {"stream", true}};
|
= {{"model", Settings::generalSettings().qrModel()}, {"stream", true}};
|
||||||
}
|
}
|
||||||
|
|
||||||
LLMCore::ContextData context = prepareContext(editor, range, instructions);
|
PluginLLMCore::ContextData context = prepareContext(editor, range, instructions);
|
||||||
|
|
||||||
bool enableTools = Settings::quickRefactorSettings().useTools();
|
bool enableTools = Settings::quickRefactorSettings().useTools();
|
||||||
bool enableThinking = Settings::quickRefactorSettings().useThinking();
|
bool enableThinking = Settings::quickRefactorSettings().useThinking();
|
||||||
@@ -169,41 +169,39 @@ void QuickRefactorHandler::prepareAndSendRequest(
|
|||||||
config.providerRequest,
|
config.providerRequest,
|
||||||
promptTemplate,
|
promptTemplate,
|
||||||
context,
|
context,
|
||||||
LLMCore::RequestType::QuickRefactoring,
|
PluginLLMCore::RequestType::QuickRefactoring,
|
||||||
enableTools,
|
enableTools,
|
||||||
enableThinking);
|
enableThinking);
|
||||||
|
|
||||||
QString requestId = QUuid::createUuid().toString();
|
|
||||||
m_lastRequestId = requestId;
|
|
||||||
QJsonObject request{{"id", requestId}};
|
|
||||||
|
|
||||||
m_isRefactoringInProgress = true;
|
m_isRefactoringInProgress = true;
|
||||||
|
|
||||||
m_activeRequests[requestId] = {request, provider};
|
|
||||||
|
|
||||||
connect(
|
connect(
|
||||||
provider,
|
provider->client(),
|
||||||
&LLMCore::Provider::fullResponseReceived,
|
&::LLMCore::BaseClient::requestCompleted,
|
||||||
this,
|
this,
|
||||||
&QuickRefactorHandler::handleFullResponse,
|
&QuickRefactorHandler::handleFullResponse,
|
||||||
Qt::UniqueConnection);
|
Qt::UniqueConnection);
|
||||||
|
|
||||||
connect(
|
connect(
|
||||||
provider,
|
provider->client(),
|
||||||
&LLMCore::Provider::requestFailed,
|
&::LLMCore::BaseClient::requestFailed,
|
||||||
this,
|
this,
|
||||||
&QuickRefactorHandler::handleRequestFailed,
|
&QuickRefactorHandler::handleRequestFailed,
|
||||||
Qt::UniqueConnection);
|
Qt::UniqueConnection);
|
||||||
|
|
||||||
provider->sendRequest(requestId, config.url, config.providerRequest);
|
auto requestId = provider->sendRequest(config.url, config.providerRequest);
|
||||||
|
m_lastRequestId = requestId;
|
||||||
|
QJsonObject request{{"id", requestId}};
|
||||||
|
|
||||||
|
m_activeRequests[requestId] = {request, provider};
|
||||||
}
|
}
|
||||||
|
|
||||||
LLMCore::ContextData QuickRefactorHandler::prepareContext(
|
PluginLLMCore::ContextData QuickRefactorHandler::prepareContext(
|
||||||
TextEditor::TextEditorWidget *editor,
|
TextEditor::TextEditorWidget *editor,
|
||||||
const Utils::Text::Range &range,
|
const Utils::Text::Range &range,
|
||||||
const QString &instructions)
|
const QString &instructions)
|
||||||
{
|
{
|
||||||
LLMCore::ContextData context;
|
PluginLLMCore::ContextData context;
|
||||||
|
|
||||||
auto textDocument = editor->textDocument();
|
auto textDocument = editor->textDocument();
|
||||||
Context::DocumentReaderQtCreator documentReader;
|
Context::DocumentReaderQtCreator documentReader;
|
||||||
@@ -287,10 +285,10 @@ LLMCore::ContextData QuickRefactorHandler::prepareContext(
|
|||||||
|
|
||||||
QString systemPrompt = Settings::quickRefactorSettings().systemPrompt();
|
QString systemPrompt = Settings::quickRefactorSettings().systemPrompt();
|
||||||
|
|
||||||
auto project = LLMCore::RulesLoader::getActiveProject();
|
auto project = PluginLLMCore::RulesLoader::getActiveProject();
|
||||||
if (project) {
|
if (project) {
|
||||||
QString projectRules = LLMCore::RulesLoader::loadRulesForProject(
|
QString projectRules = PluginLLMCore::RulesLoader::loadRulesForProject(
|
||||||
project, LLMCore::RulesContext::QuickRefactor);
|
project, PluginLLMCore::RulesContext::QuickRefactor);
|
||||||
|
|
||||||
if (!projectRules.isEmpty()) {
|
if (!projectRules.isEmpty()) {
|
||||||
systemPrompt += "\n\n# Project Rules\n\n" + projectRules;
|
systemPrompt += "\n\n# Project Rules\n\n" + projectRules;
|
||||||
@@ -368,7 +366,7 @@ LLMCore::ContextData QuickRefactorHandler::prepareContext(
|
|||||||
|
|
||||||
context.systemPrompt = systemPrompt;
|
context.systemPrompt = systemPrompt;
|
||||||
|
|
||||||
QVector<LLMCore::Message> messages;
|
QVector<PluginLLMCore::Message> messages;
|
||||||
messages.append(
|
messages.append(
|
||||||
{"user",
|
{"user",
|
||||||
instructions.isEmpty() ? "Refactor the code to improve its quality and maintainability."
|
instructions.isEmpty() ? "Refactor the code to improve its quality and maintainability."
|
||||||
@@ -387,7 +385,7 @@ void QuickRefactorHandler::handleLLMResponse(
|
|||||||
|
|
||||||
if (isComplete) {
|
if (isComplete) {
|
||||||
m_isRefactoringInProgress = false;
|
m_isRefactoringInProgress = false;
|
||||||
QString cleanedResponse = LLMCore::ResponseCleaner::clean(response);
|
QString cleanedResponse = PluginLLMCore::ResponseCleaner::clean(response);
|
||||||
|
|
||||||
RefactorResult result;
|
RefactorResult result;
|
||||||
result.newText = cleanedResponse;
|
result.newText = cleanedResponse;
|
||||||
|
|||||||
@@ -27,8 +27,8 @@
|
|||||||
|
|
||||||
#include <context/ContextManager.hpp>
|
#include <context/ContextManager.hpp>
|
||||||
#include <context/IDocumentReader.hpp>
|
#include <context/IDocumentReader.hpp>
|
||||||
#include <llmcore/ContextData.hpp>
|
#include <pluginllmcore/ContextData.hpp>
|
||||||
#include <llmcore/Provider.hpp>
|
#include <pluginllmcore/Provider.hpp>
|
||||||
|
|
||||||
namespace QodeAssist {
|
namespace QodeAssist {
|
||||||
|
|
||||||
@@ -68,7 +68,7 @@ private:
|
|||||||
const Utils::Text::Range &range);
|
const Utils::Text::Range &range);
|
||||||
|
|
||||||
void handleLLMResponse(const QString &response, const QJsonObject &request, bool isComplete);
|
void handleLLMResponse(const QString &response, const QJsonObject &request, bool isComplete);
|
||||||
LLMCore::ContextData prepareContext(
|
PluginLLMCore::ContextData prepareContext(
|
||||||
TextEditor::TextEditorWidget *editor,
|
TextEditor::TextEditorWidget *editor,
|
||||||
const Utils::Text::Range &range,
|
const Utils::Text::Range &range,
|
||||||
const QString &instructions);
|
const QString &instructions);
|
||||||
@@ -76,7 +76,7 @@ private:
|
|||||||
struct RequestContext
|
struct RequestContext
|
||||||
{
|
{
|
||||||
QJsonObject originalRequest;
|
QJsonObject originalRequest;
|
||||||
LLMCore::Provider *provider;
|
PluginLLMCore::Provider *provider;
|
||||||
};
|
};
|
||||||
|
|
||||||
QHash<QString, RequestContext> m_activeRequests;
|
QHash<QString, RequestContext> m_activeRequests;
|
||||||
|
|||||||
@@ -20,7 +20,7 @@ target_link_libraries(Context
|
|||||||
QtCreator::Utils
|
QtCreator::Utils
|
||||||
QtCreator::ProjectExplorer
|
QtCreator::ProjectExplorer
|
||||||
PRIVATE
|
PRIVATE
|
||||||
LLMCore
|
PluginLLMCore
|
||||||
QodeAssistSettings
|
QodeAssistSettings
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -269,7 +269,7 @@ CopyrightInfo DocumentContextReader::copyrightInfo() const
|
|||||||
return m_copyrightInfo;
|
return m_copyrightInfo;
|
||||||
}
|
}
|
||||||
|
|
||||||
LLMCore::ContextData DocumentContextReader::prepareContext(
|
PluginLLMCore::ContextData DocumentContextReader::prepareContext(
|
||||||
int lineNumber, int cursorPosition, const Settings::CodeCompletionSettings &settings) const
|
int lineNumber, int cursorPosition, const Settings::CodeCompletionSettings &settings) const
|
||||||
{
|
{
|
||||||
QString contextBefore;
|
QString contextBefore;
|
||||||
|
|||||||
@@ -22,7 +22,7 @@
|
|||||||
#include <texteditor/textdocument.h>
|
#include <texteditor/textdocument.h>
|
||||||
#include <QTextDocument>
|
#include <QTextDocument>
|
||||||
|
|
||||||
#include <llmcore/ContextData.hpp>
|
#include <pluginllmcore/ContextData.hpp>
|
||||||
#include <settings/CodeCompletionSettings.hpp>
|
#include <settings/CodeCompletionSettings.hpp>
|
||||||
|
|
||||||
namespace QodeAssist::Context {
|
namespace QodeAssist::Context {
|
||||||
@@ -73,7 +73,7 @@ public:
|
|||||||
|
|
||||||
CopyrightInfo copyrightInfo() const;
|
CopyrightInfo copyrightInfo() const;
|
||||||
|
|
||||||
LLMCore::ContextData prepareContext(
|
PluginLLMCore::ContextData prepareContext(
|
||||||
int lineNumber, int cursorPosition, const Settings::CodeCompletionSettings &settings) const;
|
int lineNumber, int cursorPosition, const Settings::CodeCompletionSettings &settings) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|||||||
@@ -1,36 +0,0 @@
|
|||||||
#include "Provider.hpp"
|
|
||||||
|
|
||||||
#include <QJsonDocument>
|
|
||||||
|
|
||||||
namespace QodeAssist::LLMCore {
|
|
||||||
|
|
||||||
Provider::Provider(QObject *parent)
|
|
||||||
: QObject(parent)
|
|
||||||
, m_httpClient(new HttpClient(this))
|
|
||||||
{
|
|
||||||
connect(m_httpClient, &HttpClient::dataReceived, this, &Provider::onDataReceived);
|
|
||||||
connect(m_httpClient, &HttpClient::requestFinished, this, &Provider::onRequestFinished);
|
|
||||||
}
|
|
||||||
|
|
||||||
void Provider::cancelRequest(const RequestID &requestId)
|
|
||||||
{
|
|
||||||
m_httpClient->cancelRequest(requestId);
|
|
||||||
}
|
|
||||||
|
|
||||||
HttpClient *Provider::httpClient() const
|
|
||||||
{
|
|
||||||
return m_httpClient;
|
|
||||||
}
|
|
||||||
|
|
||||||
QJsonObject Provider::parseEventLine(const QString &line)
|
|
||||||
{
|
|
||||||
if (!line.startsWith("data: "))
|
|
||||||
return QJsonObject();
|
|
||||||
|
|
||||||
QString jsonStr = line.mid(6);
|
|
||||||
|
|
||||||
QJsonDocument doc = QJsonDocument::fromJson(jsonStr.toUtf8());
|
|
||||||
return doc.object();
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace QodeAssist::LLMCore
|
|
||||||
@@ -1,118 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (C) 2024-2025 Petr Mironychev
|
|
||||||
*
|
|
||||||
* This file is part of QodeAssist.
|
|
||||||
*
|
|
||||||
* QodeAssist is free software: you can redistribute it and/or modify
|
|
||||||
* it under the terms of the GNU General Public License as published by
|
|
||||||
* the Free Software Foundation, either version 3 of the License, or
|
|
||||||
* (at your option) any later version.
|
|
||||||
*
|
|
||||||
* QodeAssist is distributed in the hope that it will be useful,
|
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
* GNU General Public License for more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU General Public License
|
|
||||||
* along with QodeAssist. If not, see <https://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#pragma once
|
|
||||||
|
|
||||||
#include <optional>
|
|
||||||
|
|
||||||
#include <QFuture>
|
|
||||||
#include <utils/environment.h>
|
|
||||||
#include <QNetworkRequest>
|
|
||||||
#include <QObject>
|
|
||||||
#include <QString>
|
|
||||||
|
|
||||||
#include "ContextData.hpp"
|
|
||||||
#include "DataBuffers.hpp"
|
|
||||||
#include "HttpClient.hpp"
|
|
||||||
#include "IToolsManager.hpp"
|
|
||||||
#include "PromptTemplate.hpp"
|
|
||||||
#include "RequestType.hpp"
|
|
||||||
|
|
||||||
class QNetworkReply;
|
|
||||||
class QJsonObject;
|
|
||||||
|
|
||||||
namespace QodeAssist::LLMCore {
|
|
||||||
|
|
||||||
class Provider : public QObject
|
|
||||||
{
|
|
||||||
Q_OBJECT
|
|
||||||
public:
|
|
||||||
explicit Provider(QObject *parent = nullptr);
|
|
||||||
|
|
||||||
virtual ~Provider() = default;
|
|
||||||
|
|
||||||
virtual QString name() const = 0;
|
|
||||||
virtual QString url() const = 0;
|
|
||||||
virtual QString completionEndpoint() const = 0;
|
|
||||||
virtual QString chatEndpoint() const = 0;
|
|
||||||
virtual bool supportsModelListing() const = 0;
|
|
||||||
virtual void prepareRequest(
|
|
||||||
QJsonObject &request,
|
|
||||||
LLMCore::PromptTemplate *prompt,
|
|
||||||
LLMCore::ContextData context,
|
|
||||||
LLMCore::RequestType type,
|
|
||||||
bool isToolsEnabled,
|
|
||||||
bool isThinkingEnabled)
|
|
||||||
= 0;
|
|
||||||
virtual QFuture<QList<QString>> getInstalledModels(const QString &url) = 0;
|
|
||||||
virtual QList<QString> validateRequest(const QJsonObject &request, TemplateType type) = 0;
|
|
||||||
virtual QString apiKey() const = 0;
|
|
||||||
virtual void prepareNetworkRequest(QNetworkRequest &networkRequest) const = 0;
|
|
||||||
virtual ProviderID providerID() const = 0;
|
|
||||||
|
|
||||||
virtual void sendRequest(const RequestID &requestId, const QUrl &url, const QJsonObject &payload)
|
|
||||||
= 0;
|
|
||||||
|
|
||||||
virtual bool supportsTools() const { return false; };
|
|
||||||
virtual bool supportThinking() const { return false; };
|
|
||||||
virtual bool supportImage() const { return false; };
|
|
||||||
|
|
||||||
virtual void cancelRequest(const RequestID &requestId);
|
|
||||||
|
|
||||||
virtual IToolsManager *toolsManager() const { return nullptr; }
|
|
||||||
|
|
||||||
HttpClient *httpClient() const;
|
|
||||||
|
|
||||||
public slots:
|
|
||||||
virtual void onDataReceived(
|
|
||||||
const QodeAssist::LLMCore::RequestID &requestId, const QByteArray &data)
|
|
||||||
= 0;
|
|
||||||
virtual void onRequestFinished(
|
|
||||||
const QodeAssist::LLMCore::RequestID &requestId, std::optional<QString> error)
|
|
||||||
= 0;
|
|
||||||
|
|
||||||
signals:
|
|
||||||
void partialResponseReceived(
|
|
||||||
const QodeAssist::LLMCore::RequestID &requestId, const QString &partialText);
|
|
||||||
void fullResponseReceived(
|
|
||||||
const QodeAssist::LLMCore::RequestID &requestId, const QString &fullText);
|
|
||||||
void requestFailed(const QodeAssist::LLMCore::RequestID &requestId, const QString &error);
|
|
||||||
void toolExecutionStarted(
|
|
||||||
const QString &requestId, const QString &toolId, const QString &toolName);
|
|
||||||
void toolExecutionCompleted(
|
|
||||||
const QString &requestId,
|
|
||||||
const QString &toolId,
|
|
||||||
const QString &toolName,
|
|
||||||
const QString &result);
|
|
||||||
void continuationStarted(const QodeAssist::LLMCore::RequestID &requestId);
|
|
||||||
void thinkingBlockReceived(
|
|
||||||
const QString &requestId, const QString &thinking, const QString &signature);
|
|
||||||
void redactedThinkingBlockReceived(const QString &requestId, const QString &signature);
|
|
||||||
|
|
||||||
protected:
|
|
||||||
QJsonObject parseEventLine(const QString &line);
|
|
||||||
|
|
||||||
QHash<RequestID, DataBuffers> m_dataBuffers;
|
|
||||||
QHash<RequestID, QUrl> m_requestUrls;
|
|
||||||
|
|
||||||
private:
|
|
||||||
HttpClient *m_httpClient;
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace QodeAssist::LLMCore
|
|
||||||
@@ -1,57 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (C) 2024-2025 Petr Mironychev
|
|
||||||
*
|
|
||||||
* This file is part of QodeAssist.
|
|
||||||
*
|
|
||||||
* QodeAssist is free software: you can redistribute it and/or modify
|
|
||||||
* it under the terms of the GNU General Public License as published by
|
|
||||||
* the Free Software Foundation, either version 3 of the License, or
|
|
||||||
* (at your option) any later version.
|
|
||||||
*
|
|
||||||
* QodeAssist is distributed in the hope that it will be useful,
|
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
* GNU General Public License for more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU General Public License
|
|
||||||
* along with QodeAssist. If not, see <https://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "ValidationUtils.hpp"
|
|
||||||
|
|
||||||
#include <QJsonArray>
|
|
||||||
|
|
||||||
namespace QodeAssist::LLMCore {
|
|
||||||
|
|
||||||
QStringList ValidationUtils::validateRequestFields(
|
|
||||||
const QJsonObject &request, const QJsonObject &templateObj)
|
|
||||||
{
|
|
||||||
QStringList errors;
|
|
||||||
validateFields(request, templateObj, errors);
|
|
||||||
validateNestedObjects(request, templateObj, errors);
|
|
||||||
return errors;
|
|
||||||
}
|
|
||||||
|
|
||||||
void ValidationUtils::validateFields(
|
|
||||||
const QJsonObject &request, const QJsonObject &templateObj, QStringList &errors)
|
|
||||||
{
|
|
||||||
for (auto it = request.begin(); it != request.end(); ++it) {
|
|
||||||
if (!templateObj.contains(it.key())) {
|
|
||||||
errors << QString("unknown field '%1'").arg(it.key());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void ValidationUtils::validateNestedObjects(
|
|
||||||
const QJsonObject &request, const QJsonObject &templateObj, QStringList &errors)
|
|
||||||
{
|
|
||||||
for (auto it = request.begin(); it != request.end(); ++it) {
|
|
||||||
if (templateObj.contains(it.key()) && it.value().isObject()
|
|
||||||
&& templateObj[it.key()].isObject()) {
|
|
||||||
validateFields(it.value().toObject(), templateObj[it.key()].toObject(), errors);
|
|
||||||
validateNestedObjects(it.value().toObject(), templateObj[it.key()].toObject(), errors);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace QodeAssist::LLMCore
|
|
||||||
@@ -19,7 +19,7 @@
|
|||||||
|
|
||||||
#include "BaseTool.hpp"
|
#include "BaseTool.hpp"
|
||||||
|
|
||||||
namespace QodeAssist::LLMCore {
|
namespace QodeAssist::PluginLLMCore {
|
||||||
|
|
||||||
BaseTool::BaseTool(QObject *parent)
|
BaseTool::BaseTool(QObject *parent)
|
||||||
: QObject(parent)
|
: QObject(parent)
|
||||||
@@ -70,4 +70,4 @@ QJsonObject BaseTool::customizeForGoogle(const QJsonObject &baseDefinition) cons
|
|||||||
return tool;
|
return tool;
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace QodeAssist::LLMCore
|
} // namespace QodeAssist::PluginLLMCore
|
||||||
@@ -25,7 +25,7 @@
|
|||||||
#include <QObject>
|
#include <QObject>
|
||||||
#include <QString>
|
#include <QString>
|
||||||
|
|
||||||
namespace QodeAssist::LLMCore {
|
namespace QodeAssist::PluginLLMCore {
|
||||||
|
|
||||||
enum class ToolSchemaFormat { OpenAI, Claude, Ollama, Google };
|
enum class ToolSchemaFormat { OpenAI, Claude, Ollama, Google };
|
||||||
|
|
||||||
@@ -67,4 +67,4 @@ protected:
|
|||||||
virtual QJsonObject customizeForGoogle(const QJsonObject &baseDefinition) const;
|
virtual QJsonObject customizeForGoogle(const QJsonObject &baseDefinition) const;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace QodeAssist::LLMCore
|
} // namespace QodeAssist::PluginLLMCore
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
add_library(LLMCore STATIC
|
add_library(PluginLLMCore STATIC
|
||||||
RequestType.hpp
|
RequestType.hpp
|
||||||
Provider.hpp Provider.cpp
|
Provider.hpp Provider.cpp
|
||||||
ProvidersManager.hpp ProvidersManager.cpp
|
ProvidersManager.hpp ProvidersManager.cpp
|
||||||
@@ -10,7 +10,6 @@ add_library(LLMCore STATIC
|
|||||||
PromptTemplate.hpp
|
PromptTemplate.hpp
|
||||||
PromptTemplateManager.hpp PromptTemplateManager.cpp
|
PromptTemplateManager.hpp PromptTemplateManager.cpp
|
||||||
RequestConfig.hpp
|
RequestConfig.hpp
|
||||||
ValidationUtils.hpp ValidationUtils.cpp
|
|
||||||
ProviderID.hpp
|
ProviderID.hpp
|
||||||
HttpClient.hpp HttpClient.cpp
|
HttpClient.hpp HttpClient.cpp
|
||||||
DataBuffers.hpp
|
DataBuffers.hpp
|
||||||
@@ -21,15 +20,16 @@ add_library(LLMCore STATIC
|
|||||||
ResponseCleaner.hpp
|
ResponseCleaner.hpp
|
||||||
)
|
)
|
||||||
|
|
||||||
target_link_libraries(LLMCore
|
target_link_libraries(PluginLLMCore
|
||||||
PUBLIC
|
PUBLIC
|
||||||
Qt::Core
|
Qt::Core
|
||||||
Qt::Network
|
Qt::Network
|
||||||
QtCreator::Core
|
QtCreator::Core
|
||||||
QtCreator::Utils
|
QtCreator::Utils
|
||||||
QtCreator::ExtensionSystem
|
QtCreator::ExtensionSystem
|
||||||
|
LLMCore
|
||||||
PRIVATE
|
PRIVATE
|
||||||
QodeAssistLogger
|
QodeAssistLogger
|
||||||
)
|
)
|
||||||
|
|
||||||
target_include_directories(LLMCore PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
|
target_include_directories(PluginLLMCore PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
|
||||||
@@ -26,7 +26,7 @@
|
|||||||
#include <QObject>
|
#include <QObject>
|
||||||
#include <QString>
|
#include <QString>
|
||||||
|
|
||||||
namespace QodeAssist::LLMCore {
|
namespace QodeAssist::PluginLLMCore {
|
||||||
|
|
||||||
enum class MessageState { Building, Complete, RequiresToolExecution, Final };
|
enum class MessageState { Building, Complete, RequiresToolExecution, Final };
|
||||||
|
|
||||||
@@ -249,4 +249,4 @@ private:
|
|||||||
QString m_signature;
|
QString m_signature;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace QodeAssist::LLMCore
|
} // namespace QodeAssist::PluginLLMCore
|
||||||
@@ -22,7 +22,7 @@
|
|||||||
#include <QString>
|
#include <QString>
|
||||||
#include <QVector>
|
#include <QVector>
|
||||||
|
|
||||||
namespace QodeAssist::LLMCore {
|
namespace QodeAssist::PluginLLMCore {
|
||||||
|
|
||||||
struct ImageAttachment
|
struct ImageAttachment
|
||||||
{
|
{
|
||||||
@@ -66,4 +66,4 @@ struct ContextData
|
|||||||
bool operator==(const ContextData &) const = default;
|
bool operator==(const ContextData &) const = default;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace QodeAssist::LLMCore
|
} // namespace QodeAssist::PluginLLMCore
|
||||||
@@ -22,7 +22,7 @@
|
|||||||
#include "SSEBuffer.hpp"
|
#include "SSEBuffer.hpp"
|
||||||
#include <QString>
|
#include <QString>
|
||||||
|
|
||||||
namespace QodeAssist::LLMCore {
|
namespace QodeAssist::PluginLLMCore {
|
||||||
|
|
||||||
struct DataBuffers
|
struct DataBuffers
|
||||||
{
|
{
|
||||||
@@ -36,4 +36,4 @@ struct DataBuffers
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace QodeAssist::LLMCore
|
} // namespace QodeAssist::PluginLLMCore
|
||||||
@@ -24,7 +24,7 @@
|
|||||||
|
|
||||||
#include <Logger.hpp>
|
#include <Logger.hpp>
|
||||||
|
|
||||||
namespace QodeAssist::LLMCore {
|
namespace QodeAssist::PluginLLMCore {
|
||||||
|
|
||||||
HttpClient::HttpClient(QObject *parent)
|
HttpClient::HttpClient(QObject *parent)
|
||||||
: QObject(parent)
|
: QObject(parent)
|
||||||
@@ -273,4 +273,4 @@ QString HttpClient::parseErrorFromResponse(
|
|||||||
return QString("HTTP %1: %2").arg(statusCode).arg(networkErrorString);
|
return QString("HTTP %1: %2").arg(statusCode).arg(networkErrorString);
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace QodeAssist::LLMCore
|
} // namespace QodeAssist::PluginLLMCore
|
||||||
@@ -30,7 +30,7 @@
|
|||||||
#include <QObject>
|
#include <QObject>
|
||||||
#include <QPromise>
|
#include <QPromise>
|
||||||
|
|
||||||
namespace QodeAssist::LLMCore {
|
namespace QodeAssist::PluginLLMCore {
|
||||||
|
|
||||||
class HttpClient : public QObject
|
class HttpClient : public QObject
|
||||||
{
|
{
|
||||||
@@ -73,4 +73,4 @@ private:
|
|||||||
mutable QMutex m_mutex;
|
mutable QMutex m_mutex;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace QodeAssist::LLMCore
|
} // namespace QodeAssist::PluginLLMCore
|
||||||
@@ -22,7 +22,7 @@
|
|||||||
#include "PromptTemplate.hpp"
|
#include "PromptTemplate.hpp"
|
||||||
#include <QString>
|
#include <QString>
|
||||||
|
|
||||||
namespace QodeAssist::LLMCore {
|
namespace QodeAssist::PluginLLMCore {
|
||||||
|
|
||||||
class IPromptProvider
|
class IPromptProvider
|
||||||
{
|
{
|
||||||
@@ -36,4 +36,4 @@ public:
|
|||||||
virtual QStringList getTemplatesForProvider(ProviderID id) const = 0;
|
virtual QStringList getTemplatesForProvider(ProviderID id) const = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace QodeAssist::LLMCore
|
} // namespace QodeAssist::PluginLLMCore
|
||||||
@@ -21,7 +21,7 @@
|
|||||||
|
|
||||||
#include "Provider.hpp"
|
#include "Provider.hpp"
|
||||||
|
|
||||||
namespace QodeAssist::LLMCore {
|
namespace QodeAssist::PluginLLMCore {
|
||||||
|
|
||||||
class IProviderRegistry
|
class IProviderRegistry
|
||||||
{
|
{
|
||||||
@@ -33,4 +33,4 @@ public:
|
|||||||
virtual QStringList providersNames() const = 0;
|
virtual QStringList providersNames() const = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace QodeAssist::LLMCore
|
} // namespace QodeAssist::PluginLLMCore
|
||||||
@@ -26,7 +26,7 @@
|
|||||||
|
|
||||||
#include "BaseTool.hpp"
|
#include "BaseTool.hpp"
|
||||||
|
|
||||||
namespace QodeAssist::LLMCore {
|
namespace QodeAssist::PluginLLMCore {
|
||||||
|
|
||||||
class IToolsManager
|
class IToolsManager
|
||||||
{
|
{
|
||||||
@@ -23,7 +23,7 @@
|
|||||||
#include "PromptTemplate.hpp"
|
#include "PromptTemplate.hpp"
|
||||||
#include "PromptTemplateManager.hpp"
|
#include "PromptTemplateManager.hpp"
|
||||||
|
|
||||||
namespace QodeAssist::LLMCore {
|
namespace QodeAssist::PluginLLMCore {
|
||||||
|
|
||||||
class PromptProviderChat : public IPromptProvider
|
class PromptProviderChat : public IPromptProvider
|
||||||
{
|
{
|
||||||
@@ -50,4 +50,4 @@ private:
|
|||||||
PromptTemplateManager &m_templateManager;
|
PromptTemplateManager &m_templateManager;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace QodeAssist::LLMCore
|
} // namespace QodeAssist::PluginLLMCore
|
||||||
@@ -22,7 +22,7 @@
|
|||||||
#include "IPromptProvider.hpp"
|
#include "IPromptProvider.hpp"
|
||||||
#include "PromptTemplateManager.hpp"
|
#include "PromptTemplateManager.hpp"
|
||||||
|
|
||||||
namespace QodeAssist::LLMCore {
|
namespace QodeAssist::PluginLLMCore {
|
||||||
|
|
||||||
class PromptProviderFim : public IPromptProvider
|
class PromptProviderFim : public IPromptProvider
|
||||||
{
|
{
|
||||||
@@ -49,4 +49,4 @@ private:
|
|||||||
PromptTemplateManager &m_templateManager;
|
PromptTemplateManager &m_templateManager;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace QodeAssist::LLMCore
|
} // namespace QodeAssist::PluginLLMCore
|
||||||
@@ -26,7 +26,7 @@
|
|||||||
#include "ContextData.hpp"
|
#include "ContextData.hpp"
|
||||||
#include "ProviderID.hpp"
|
#include "ProviderID.hpp"
|
||||||
|
|
||||||
namespace QodeAssist::LLMCore {
|
namespace QodeAssist::PluginLLMCore {
|
||||||
|
|
||||||
enum class TemplateType { Chat, FIM, FIMOnChat };
|
enum class TemplateType { Chat, FIM, FIMOnChat };
|
||||||
|
|
||||||
@@ -41,4 +41,4 @@ public:
|
|||||||
virtual QString description() const = 0;
|
virtual QString description() const = 0;
|
||||||
virtual bool isSupportProvider(ProviderID id) const = 0;
|
virtual bool isSupportProvider(ProviderID id) const = 0;
|
||||||
};
|
};
|
||||||
} // namespace QodeAssist::LLMCore
|
} // namespace QodeAssist::PluginLLMCore
|
||||||
@@ -21,7 +21,7 @@
|
|||||||
|
|
||||||
#include <QMessageBox>
|
#include <QMessageBox>
|
||||||
|
|
||||||
namespace QodeAssist::LLMCore {
|
namespace QodeAssist::PluginLLMCore {
|
||||||
|
|
||||||
PromptTemplateManager &PromptTemplateManager::instance()
|
PromptTemplateManager &PromptTemplateManager::instance()
|
||||||
{
|
{
|
||||||
@@ -96,4 +96,4 @@ PromptTemplate *PromptTemplateManager::getChatTemplateByName(const QString &temp
|
|||||||
return m_chatTemplates[templateName];
|
return m_chatTemplates[templateName];
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace QodeAssist::LLMCore
|
} // namespace QodeAssist::PluginLLMCore
|
||||||
@@ -24,7 +24,7 @@
|
|||||||
|
|
||||||
#include "PromptTemplate.hpp"
|
#include "PromptTemplate.hpp"
|
||||||
|
|
||||||
namespace QodeAssist::LLMCore {
|
namespace QodeAssist::PluginLLMCore {
|
||||||
|
|
||||||
class PromptTemplateManager
|
class PromptTemplateManager
|
||||||
{
|
{
|
||||||
@@ -62,4 +62,4 @@ private:
|
|||||||
QMap<QString, PromptTemplate *> m_chatTemplates;
|
QMap<QString, PromptTemplate *> m_chatTemplates;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace QodeAssist::LLMCore
|
} // namespace QodeAssist::PluginLLMCore
|
||||||
61
pluginllmcore/Provider.cpp
Normal file
61
pluginllmcore/Provider.cpp
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2024-2025 Petr Mironychev
|
||||||
|
*
|
||||||
|
* This file is part of QodeAssist.
|
||||||
|
*
|
||||||
|
* QodeAssist is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* QodeAssist is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with QodeAssist. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "Provider.hpp"
|
||||||
|
|
||||||
|
#include <LLMCore/BaseClient.hpp>
|
||||||
|
#include <LLMCore/ToolsManager.hpp>
|
||||||
|
|
||||||
|
#include <Logger.hpp>
|
||||||
|
|
||||||
|
namespace QodeAssist::PluginLLMCore {
|
||||||
|
|
||||||
|
Provider::Provider(QObject *parent)
|
||||||
|
: QObject(parent)
|
||||||
|
{}
|
||||||
|
|
||||||
|
RequestID Provider::sendRequest(const QUrl &url, const QJsonObject &payload)
|
||||||
|
{
|
||||||
|
auto *c = client();
|
||||||
|
|
||||||
|
QUrl baseUrl(url);
|
||||||
|
baseUrl.setPath("");
|
||||||
|
c->setUrl(baseUrl.toString());
|
||||||
|
c->setApiKey(apiKey());
|
||||||
|
|
||||||
|
auto requestId = c->sendMessage(payload);
|
||||||
|
|
||||||
|
LOG_MESSAGE(
|
||||||
|
QString("%1: Sending request %2 to %3").arg(name(), requestId, url.toString()));
|
||||||
|
|
||||||
|
return requestId;
|
||||||
|
}
|
||||||
|
|
||||||
|
void Provider::cancelRequest(const RequestID &requestId)
|
||||||
|
{
|
||||||
|
LOG_MESSAGE(QString("%1: Cancelling request %2").arg(name(), requestId));
|
||||||
|
client()->cancelRequest(requestId);
|
||||||
|
}
|
||||||
|
|
||||||
|
::LLMCore::ToolsManager *Provider::toolsManager() const
|
||||||
|
{
|
||||||
|
return client()->tools();
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace QodeAssist::PluginLLMCore
|
||||||
83
pluginllmcore/Provider.hpp
Normal file
83
pluginllmcore/Provider.hpp
Normal file
@@ -0,0 +1,83 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 2024-2025 Petr Mironychev
|
||||||
|
*
|
||||||
|
* This file is part of QodeAssist.
|
||||||
|
*
|
||||||
|
* QodeAssist is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* QodeAssist is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with QodeAssist. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <QFlags>
|
||||||
|
#include <QFuture>
|
||||||
|
#include <QObject>
|
||||||
|
#include <QString>
|
||||||
|
#include <utils/environment.h>
|
||||||
|
|
||||||
|
#include "ContextData.hpp"
|
||||||
|
#include "IToolsManager.hpp"
|
||||||
|
#include "PromptTemplate.hpp"
|
||||||
|
#include "RequestType.hpp"
|
||||||
|
|
||||||
|
namespace LLMCore {
|
||||||
|
class BaseClient;
|
||||||
|
class ToolsManager;
|
||||||
|
}
|
||||||
|
|
||||||
|
class QJsonObject;
|
||||||
|
|
||||||
|
namespace QodeAssist::PluginLLMCore {
|
||||||
|
|
||||||
|
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
|
||||||
|
public:
|
||||||
|
explicit Provider(QObject *parent = nullptr);
|
||||||
|
|
||||||
|
virtual ~Provider() = default;
|
||||||
|
|
||||||
|
virtual QString name() const = 0;
|
||||||
|
virtual QString url() const = 0;
|
||||||
|
virtual QString completionEndpoint() const = 0;
|
||||||
|
virtual QString chatEndpoint() const = 0;
|
||||||
|
virtual void prepareRequest(
|
||||||
|
QJsonObject &request,
|
||||||
|
PluginLLMCore::PromptTemplate *prompt,
|
||||||
|
PluginLLMCore::ContextData context,
|
||||||
|
PluginLLMCore::RequestType type,
|
||||||
|
bool isToolsEnabled,
|
||||||
|
bool isThinkingEnabled)
|
||||||
|
= 0;
|
||||||
|
virtual QFuture<QList<QString>> getInstalledModels(const QString &url) = 0;
|
||||||
|
virtual ProviderID providerID() const = 0;
|
||||||
|
virtual ProviderCapabilities capabilities() const { return {}; }
|
||||||
|
|
||||||
|
virtual ::LLMCore::BaseClient *client() const = 0;
|
||||||
|
virtual QString apiKey() const = 0;
|
||||||
|
|
||||||
|
RequestID sendRequest(const QUrl &url, const QJsonObject &payload);
|
||||||
|
void cancelRequest(const RequestID &requestId);
|
||||||
|
::LLMCore::ToolsManager *toolsManager() const;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace QodeAssist::PluginLLMCore
|
||||||
@@ -17,7 +17,7 @@
|
|||||||
* along with QodeAssist. If not, see <https://www.gnu.org/licenses/>.
|
* along with QodeAssist. If not, see <https://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
namespace QodeAssist::LLMCore {
|
namespace QodeAssist::PluginLLMCore {
|
||||||
|
|
||||||
enum class ProviderID {
|
enum class ProviderID {
|
||||||
Any,
|
Any,
|
||||||
@@ -19,7 +19,7 @@
|
|||||||
|
|
||||||
#include "ProvidersManager.hpp"
|
#include "ProvidersManager.hpp"
|
||||||
|
|
||||||
namespace QodeAssist::LLMCore {
|
namespace QodeAssist::PluginLLMCore {
|
||||||
|
|
||||||
ProvidersManager &ProvidersManager::instance()
|
ProvidersManager &ProvidersManager::instance()
|
||||||
{
|
{
|
||||||
@@ -44,4 +44,4 @@ Provider *ProvidersManager::getProviderByName(const QString &providerName)
|
|||||||
return m_providers[providerName];
|
return m_providers[providerName];
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace QodeAssist::LLMCore
|
} // namespace QodeAssist::PluginLLMCore
|
||||||
@@ -24,7 +24,7 @@
|
|||||||
#include "IProviderRegistry.hpp"
|
#include "IProviderRegistry.hpp"
|
||||||
#include <QMap>
|
#include <QMap>
|
||||||
|
|
||||||
namespace QodeAssist::LLMCore {
|
namespace QodeAssist::PluginLLMCore {
|
||||||
|
|
||||||
class ProvidersManager : public IProviderRegistry
|
class ProvidersManager : public IProviderRegistry
|
||||||
{
|
{
|
||||||
@@ -53,4 +53,4 @@ private:
|
|||||||
QMap<QString, Provider *> m_providers;
|
QMap<QString, Provider *> m_providers;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace QodeAssist::LLMCore
|
} // namespace QodeAssist::PluginLLMCore
|
||||||
@@ -25,7 +25,7 @@
|
|||||||
#include <QJsonObject>
|
#include <QJsonObject>
|
||||||
#include <QUrl>
|
#include <QUrl>
|
||||||
|
|
||||||
namespace QodeAssist::LLMCore {
|
namespace QodeAssist::PluginLLMCore {
|
||||||
|
|
||||||
struct LLMConfig
|
struct LLMConfig
|
||||||
{
|
{
|
||||||
@@ -35,7 +35,6 @@ struct LLMConfig
|
|||||||
QJsonObject providerRequest;
|
QJsonObject providerRequest;
|
||||||
RequestType requestType;
|
RequestType requestType;
|
||||||
bool multiLineCompletion;
|
bool multiLineCompletion;
|
||||||
QString apiKey;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace QodeAssist::LLMCore
|
} // namespace QodeAssist::PluginLLMCore
|
||||||
@@ -21,7 +21,7 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
namespace QodeAssist::LLMCore {
|
namespace QodeAssist::PluginLLMCore {
|
||||||
|
|
||||||
enum RequestType { CodeCompletion, Chat, Embedding, QuickRefactoring };
|
enum RequestType { CodeCompletion, Chat, Embedding, QuickRefactoring };
|
||||||
|
|
||||||
@@ -23,7 +23,7 @@
|
|||||||
#include <QStringList>
|
#include <QStringList>
|
||||||
#include <QRegularExpression>
|
#include <QRegularExpression>
|
||||||
|
|
||||||
namespace QodeAssist::LLMCore {
|
namespace QodeAssist::PluginLLMCore {
|
||||||
|
|
||||||
class ResponseCleaner
|
class ResponseCleaner
|
||||||
{
|
{
|
||||||
@@ -115,5 +115,5 @@ private:
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace QodeAssist::LLMCore
|
} // namespace QodeAssist::PluginLLMCore
|
||||||
|
|
||||||
@@ -26,7 +26,7 @@
|
|||||||
#include <projectexplorer/project.h>
|
#include <projectexplorer/project.h>
|
||||||
#include <projectexplorer/projectmanager.h>
|
#include <projectexplorer/projectmanager.h>
|
||||||
|
|
||||||
namespace QodeAssist::LLMCore {
|
namespace QodeAssist::PluginLLMCore {
|
||||||
|
|
||||||
QString RulesLoader::loadRules(const QString &projectPath, RulesContext context)
|
QString RulesLoader::loadRules(const QString &projectPath, RulesContext context)
|
||||||
{
|
{
|
||||||
@@ -178,4 +178,4 @@ QVector<RuleFileInfo> RulesLoader::collectMarkdownFiles(
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace QodeAssist::LLMCore
|
} // namespace QodeAssist::PluginLLMCore
|
||||||
@@ -25,7 +25,7 @@ namespace ProjectExplorer {
|
|||||||
class Project;
|
class Project;
|
||||||
}
|
}
|
||||||
|
|
||||||
namespace QodeAssist::LLMCore {
|
namespace QodeAssist::PluginLLMCore {
|
||||||
|
|
||||||
enum class RulesContext { Completions, Chat, QuickRefactor };
|
enum class RulesContext { Completions, Chat, QuickRefactor };
|
||||||
|
|
||||||
@@ -54,4 +54,4 @@ private:
|
|||||||
static QString getProjectPath(ProjectExplorer::Project *project);
|
static QString getProjectPath(ProjectExplorer::Project *project);
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace QodeAssist::LLMCore
|
} // namespace QodeAssist::PluginLLMCore
|
||||||
@@ -19,7 +19,7 @@
|
|||||||
|
|
||||||
#include "SSEBuffer.hpp"
|
#include "SSEBuffer.hpp"
|
||||||
|
|
||||||
namespace QodeAssist::LLMCore {
|
namespace QodeAssist::PluginLLMCore {
|
||||||
|
|
||||||
QStringList SSEBuffer::processData(const QByteArray &data)
|
QStringList SSEBuffer::processData(const QByteArray &data)
|
||||||
{
|
{
|
||||||
@@ -48,4 +48,4 @@ bool SSEBuffer::hasIncompleteData() const
|
|||||||
return !m_buffer.isEmpty();
|
return !m_buffer.isEmpty();
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace QodeAssist::LLMCore
|
} // namespace QodeAssist::PluginLLMCore
|
||||||
@@ -22,7 +22,7 @@
|
|||||||
#include <QString>
|
#include <QString>
|
||||||
#include <QStringList>
|
#include <QStringList>
|
||||||
|
|
||||||
namespace QodeAssist::LLMCore {
|
namespace QodeAssist::PluginLLMCore {
|
||||||
|
|
||||||
class SSEBuffer
|
class SSEBuffer
|
||||||
{
|
{
|
||||||
@@ -39,4 +39,4 @@ private:
|
|||||||
QString m_buffer;
|
QString m_buffer;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace QodeAssist::LLMCore
|
} // namespace QodeAssist::PluginLLMCore
|
||||||
@@ -37,32 +37,32 @@ void ClaudeMessage::handleContentBlockStart(
|
|||||||
.arg(blockType));
|
.arg(blockType));
|
||||||
|
|
||||||
if (blockType == "text") {
|
if (blockType == "text") {
|
||||||
addCurrentContent<LLMCore::TextContent>();
|
addCurrentContent<PluginLLMCore::TextContent>();
|
||||||
|
|
||||||
} else if (blockType == "image") {
|
} else if (blockType == "image") {
|
||||||
QJsonObject source = data["source"].toObject();
|
QJsonObject source = data["source"].toObject();
|
||||||
QString sourceType = source["type"].toString();
|
QString sourceType = source["type"].toString();
|
||||||
QString imageData;
|
QString imageData;
|
||||||
QString mediaType;
|
QString mediaType;
|
||||||
LLMCore::ImageContent::ImageSourceType imgSourceType = LLMCore::ImageContent::ImageSourceType::Base64;
|
PluginLLMCore::ImageContent::ImageSourceType imgSourceType = PluginLLMCore::ImageContent::ImageSourceType::Base64;
|
||||||
|
|
||||||
if (sourceType == "base64") {
|
if (sourceType == "base64") {
|
||||||
imageData = source["data"].toString();
|
imageData = source["data"].toString();
|
||||||
mediaType = source["media_type"].toString();
|
mediaType = source["media_type"].toString();
|
||||||
imgSourceType = LLMCore::ImageContent::ImageSourceType::Base64;
|
imgSourceType = PluginLLMCore::ImageContent::ImageSourceType::Base64;
|
||||||
} else if (sourceType == "url") {
|
} else if (sourceType == "url") {
|
||||||
imageData = source["url"].toString();
|
imageData = source["url"].toString();
|
||||||
imgSourceType = LLMCore::ImageContent::ImageSourceType::Url;
|
imgSourceType = PluginLLMCore::ImageContent::ImageSourceType::Url;
|
||||||
}
|
}
|
||||||
|
|
||||||
addCurrentContent<LLMCore::ImageContent>(imageData, mediaType, imgSourceType);
|
addCurrentContent<PluginLLMCore::ImageContent>(imageData, mediaType, imgSourceType);
|
||||||
|
|
||||||
} else if (blockType == "tool_use") {
|
} else if (blockType == "tool_use") {
|
||||||
QString toolId = data["id"].toString();
|
QString toolId = data["id"].toString();
|
||||||
QString toolName = data["name"].toString();
|
QString toolName = data["name"].toString();
|
||||||
QJsonObject toolInput = data["input"].toObject();
|
QJsonObject toolInput = data["input"].toObject();
|
||||||
|
|
||||||
addCurrentContent<LLMCore::ToolUseContent>(toolId, toolName, toolInput);
|
addCurrentContent<PluginLLMCore::ToolUseContent>(toolId, toolName, toolInput);
|
||||||
m_pendingToolInputs[index] = "";
|
m_pendingToolInputs[index] = "";
|
||||||
|
|
||||||
} else if (blockType == "thinking") {
|
} else if (blockType == "thinking") {
|
||||||
@@ -70,13 +70,13 @@ void ClaudeMessage::handleContentBlockStart(
|
|||||||
QString signature = data["signature"].toString();
|
QString signature = data["signature"].toString();
|
||||||
LOG_MESSAGE(QString("ClaudeMessage: Creating thinking block with signature length=%1")
|
LOG_MESSAGE(QString("ClaudeMessage: Creating thinking block with signature length=%1")
|
||||||
.arg(signature.length()));
|
.arg(signature.length()));
|
||||||
addCurrentContent<LLMCore::ThinkingContent>(thinking, signature);
|
addCurrentContent<PluginLLMCore::ThinkingContent>(thinking, signature);
|
||||||
|
|
||||||
} else if (blockType == "redacted_thinking") {
|
} else if (blockType == "redacted_thinking") {
|
||||||
QString signature = data["signature"].toString();
|
QString signature = data["signature"].toString();
|
||||||
LOG_MESSAGE(QString("ClaudeMessage: Creating redacted_thinking block with signature length=%1")
|
LOG_MESSAGE(QString("ClaudeMessage: Creating redacted_thinking block with signature length=%1")
|
||||||
.arg(signature.length()));
|
.arg(signature.length()));
|
||||||
addCurrentContent<LLMCore::RedactedThinkingContent>(signature);
|
addCurrentContent<PluginLLMCore::RedactedThinkingContent>(signature);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -88,7 +88,7 @@ void ClaudeMessage::handleContentBlockDelta(
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (deltaType == "text_delta") {
|
if (deltaType == "text_delta") {
|
||||||
if (auto textContent = qobject_cast<LLMCore::TextContent *>(m_currentBlocks[index])) {
|
if (auto textContent = qobject_cast<PluginLLMCore::TextContent *>(m_currentBlocks[index])) {
|
||||||
textContent->appendText(delta["text"].toString());
|
textContent->appendText(delta["text"].toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -99,17 +99,17 @@ void ClaudeMessage::handleContentBlockDelta(
|
|||||||
}
|
}
|
||||||
|
|
||||||
} else if (deltaType == "thinking_delta") {
|
} else if (deltaType == "thinking_delta") {
|
||||||
if (auto thinkingContent = qobject_cast<LLMCore::ThinkingContent *>(m_currentBlocks[index])) {
|
if (auto thinkingContent = qobject_cast<PluginLLMCore::ThinkingContent *>(m_currentBlocks[index])) {
|
||||||
thinkingContent->appendThinking(delta["thinking"].toString());
|
thinkingContent->appendThinking(delta["thinking"].toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
} else if (deltaType == "signature_delta") {
|
} else if (deltaType == "signature_delta") {
|
||||||
if (auto thinkingContent = qobject_cast<LLMCore::ThinkingContent *>(m_currentBlocks[index])) {
|
if (auto thinkingContent = qobject_cast<PluginLLMCore::ThinkingContent *>(m_currentBlocks[index])) {
|
||||||
QString signature = delta["signature"].toString();
|
QString signature = delta["signature"].toString();
|
||||||
thinkingContent->setSignature(signature);
|
thinkingContent->setSignature(signature);
|
||||||
LOG_MESSAGE(QString("Set signature for thinking block %1: length=%2")
|
LOG_MESSAGE(QString("Set signature for thinking block %1: length=%2")
|
||||||
.arg(index).arg(signature.length()));
|
.arg(index).arg(signature.length()));
|
||||||
} else if (auto redactedContent = qobject_cast<LLMCore::RedactedThinkingContent *>(m_currentBlocks[index])) {
|
} else if (auto redactedContent = qobject_cast<PluginLLMCore::RedactedThinkingContent *>(m_currentBlocks[index])) {
|
||||||
QString signature = delta["signature"].toString();
|
QString signature = delta["signature"].toString();
|
||||||
redactedContent->setSignature(signature);
|
redactedContent->setSignature(signature);
|
||||||
LOG_MESSAGE(QString("Set signature for redacted_thinking block %1: length=%2")
|
LOG_MESSAGE(QString("Set signature for redacted_thinking block %1: length=%2")
|
||||||
@@ -132,7 +132,7 @@ void ClaudeMessage::handleContentBlockStop(int index)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (index < m_currentBlocks.size()) {
|
if (index < m_currentBlocks.size()) {
|
||||||
if (auto toolContent = qobject_cast<LLMCore::ToolUseContent *>(m_currentBlocks[index])) {
|
if (auto toolContent = qobject_cast<PluginLLMCore::ToolUseContent *>(m_currentBlocks[index])) {
|
||||||
toolContent->setInput(inputObject);
|
toolContent->setInput(inputObject);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -155,7 +155,7 @@ QJsonObject ClaudeMessage::toProviderFormat() const
|
|||||||
QJsonArray content;
|
QJsonArray content;
|
||||||
|
|
||||||
for (auto block : m_currentBlocks) {
|
for (auto block : m_currentBlocks) {
|
||||||
QJsonValue blockJson = block->toJson(LLMCore::ProviderFormat::Claude);
|
QJsonValue blockJson = block->toJson(PluginLLMCore::ProviderFormat::Claude);
|
||||||
content.append(blockJson);
|
content.append(blockJson);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -173,42 +173,42 @@ QJsonArray ClaudeMessage::createToolResultsContent(const QHash<QString, QString>
|
|||||||
|
|
||||||
for (auto toolContent : getCurrentToolUseContent()) {
|
for (auto toolContent : getCurrentToolUseContent()) {
|
||||||
if (toolResults.contains(toolContent->id())) {
|
if (toolResults.contains(toolContent->id())) {
|
||||||
auto toolResult = std::make_unique<LLMCore::ToolResultContent>(
|
auto toolResult = std::make_unique<PluginLLMCore::ToolResultContent>(
|
||||||
toolContent->id(), toolResults[toolContent->id()]);
|
toolContent->id(), toolResults[toolContent->id()]);
|
||||||
results.append(toolResult->toJson(LLMCore::ProviderFormat::Claude));
|
results.append(toolResult->toJson(PluginLLMCore::ProviderFormat::Claude));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return results;
|
return results;
|
||||||
}
|
}
|
||||||
|
|
||||||
QList<LLMCore::ToolUseContent *> ClaudeMessage::getCurrentToolUseContent() const
|
QList<PluginLLMCore::ToolUseContent *> ClaudeMessage::getCurrentToolUseContent() const
|
||||||
{
|
{
|
||||||
QList<LLMCore::ToolUseContent *> toolBlocks;
|
QList<PluginLLMCore::ToolUseContent *> toolBlocks;
|
||||||
for (auto block : m_currentBlocks) {
|
for (auto block : m_currentBlocks) {
|
||||||
if (auto toolContent = qobject_cast<LLMCore::ToolUseContent *>(block)) {
|
if (auto toolContent = qobject_cast<PluginLLMCore::ToolUseContent *>(block)) {
|
||||||
toolBlocks.append(toolContent);
|
toolBlocks.append(toolContent);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return toolBlocks;
|
return toolBlocks;
|
||||||
}
|
}
|
||||||
|
|
||||||
QList<LLMCore::ThinkingContent *> ClaudeMessage::getCurrentThinkingContent() const
|
QList<PluginLLMCore::ThinkingContent *> ClaudeMessage::getCurrentThinkingContent() const
|
||||||
{
|
{
|
||||||
QList<LLMCore::ThinkingContent *> thinkingBlocks;
|
QList<PluginLLMCore::ThinkingContent *> thinkingBlocks;
|
||||||
for (auto block : m_currentBlocks) {
|
for (auto block : m_currentBlocks) {
|
||||||
if (auto thinkingContent = qobject_cast<LLMCore::ThinkingContent *>(block)) {
|
if (auto thinkingContent = qobject_cast<PluginLLMCore::ThinkingContent *>(block)) {
|
||||||
thinkingBlocks.append(thinkingContent);
|
thinkingBlocks.append(thinkingContent);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return thinkingBlocks;
|
return thinkingBlocks;
|
||||||
}
|
}
|
||||||
|
|
||||||
QList<LLMCore::RedactedThinkingContent *> ClaudeMessage::getCurrentRedactedThinkingContent() const
|
QList<PluginLLMCore::RedactedThinkingContent *> ClaudeMessage::getCurrentRedactedThinkingContent() const
|
||||||
{
|
{
|
||||||
QList<LLMCore::RedactedThinkingContent *> redactedBlocks;
|
QList<PluginLLMCore::RedactedThinkingContent *> redactedBlocks;
|
||||||
for (auto block : m_currentBlocks) {
|
for (auto block : m_currentBlocks) {
|
||||||
if (auto redactedContent = qobject_cast<LLMCore::RedactedThinkingContent *>(block)) {
|
if (auto redactedContent = qobject_cast<PluginLLMCore::RedactedThinkingContent *>(block)) {
|
||||||
redactedBlocks.append(redactedContent);
|
redactedBlocks.append(redactedContent);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -222,17 +222,17 @@ void ClaudeMessage::startNewContinuation()
|
|||||||
m_currentBlocks.clear();
|
m_currentBlocks.clear();
|
||||||
m_pendingToolInputs.clear();
|
m_pendingToolInputs.clear();
|
||||||
m_stopReason.clear();
|
m_stopReason.clear();
|
||||||
m_state = LLMCore::MessageState::Building;
|
m_state = PluginLLMCore::MessageState::Building;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ClaudeMessage::updateStateFromStopReason()
|
void ClaudeMessage::updateStateFromStopReason()
|
||||||
{
|
{
|
||||||
if (m_stopReason == "tool_use" && !getCurrentToolUseContent().empty()) {
|
if (m_stopReason == "tool_use" && !getCurrentToolUseContent().empty()) {
|
||||||
m_state = LLMCore::MessageState::RequiresToolExecution;
|
m_state = PluginLLMCore::MessageState::RequiresToolExecution;
|
||||||
} else if (m_stopReason == "end_turn") {
|
} else if (m_stopReason == "end_turn") {
|
||||||
m_state = LLMCore::MessageState::Final;
|
m_state = PluginLLMCore::MessageState::Final;
|
||||||
} else {
|
} else {
|
||||||
m_state = LLMCore::MessageState::Complete;
|
m_state = PluginLLMCore::MessageState::Complete;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -19,7 +19,7 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <llmcore/ContentBlocks.hpp>
|
#include <pluginllmcore/ContentBlocks.hpp>
|
||||||
|
|
||||||
namespace QodeAssist {
|
namespace QodeAssist {
|
||||||
|
|
||||||
@@ -37,18 +37,18 @@ public:
|
|||||||
QJsonObject toProviderFormat() const;
|
QJsonObject toProviderFormat() const;
|
||||||
QJsonArray createToolResultsContent(const QHash<QString, QString> &toolResults) const;
|
QJsonArray createToolResultsContent(const QHash<QString, QString> &toolResults) const;
|
||||||
|
|
||||||
LLMCore::MessageState state() const { return m_state; }
|
PluginLLMCore::MessageState state() const { return m_state; }
|
||||||
QList<LLMCore::ToolUseContent *> getCurrentToolUseContent() const;
|
QList<PluginLLMCore::ToolUseContent *> getCurrentToolUseContent() const;
|
||||||
QList<LLMCore::ThinkingContent *> getCurrentThinkingContent() const;
|
QList<PluginLLMCore::ThinkingContent *> getCurrentThinkingContent() const;
|
||||||
QList<LLMCore::RedactedThinkingContent *> getCurrentRedactedThinkingContent() const;
|
QList<PluginLLMCore::RedactedThinkingContent *> getCurrentRedactedThinkingContent() const;
|
||||||
const QList<LLMCore::ContentBlock *> &getCurrentBlocks() const { return m_currentBlocks; }
|
const QList<PluginLLMCore::ContentBlock *> &getCurrentBlocks() const { return m_currentBlocks; }
|
||||||
|
|
||||||
void startNewContinuation();
|
void startNewContinuation();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QString m_stopReason;
|
QString m_stopReason;
|
||||||
LLMCore::MessageState m_state = LLMCore::MessageState::Building;
|
PluginLLMCore::MessageState m_state = PluginLLMCore::MessageState::Building;
|
||||||
QList<LLMCore::ContentBlock *> m_currentBlocks;
|
QList<PluginLLMCore::ContentBlock *> m_currentBlocks;
|
||||||
QHash<int, QString> m_pendingToolInputs;
|
QHash<int, QString> m_pendingToolInputs;
|
||||||
|
|
||||||
void updateStateFromStopReason();
|
void updateStateFromStopReason();
|
||||||
|
|||||||
@@ -22,27 +22,24 @@
|
|||||||
#include <QJsonArray>
|
#include <QJsonArray>
|
||||||
#include <QJsonDocument>
|
#include <QJsonDocument>
|
||||||
#include <QJsonObject>
|
#include <QJsonObject>
|
||||||
#include <QUrlQuery>
|
|
||||||
|
|
||||||
#include "llmcore/ValidationUtils.hpp"
|
#include <LLMCore/ToolsManager.hpp>
|
||||||
|
|
||||||
#include "logger/Logger.hpp"
|
#include "logger/Logger.hpp"
|
||||||
#include "settings/ChatAssistantSettings.hpp"
|
#include "settings/ChatAssistantSettings.hpp"
|
||||||
#include "settings/CodeCompletionSettings.hpp"
|
#include "settings/CodeCompletionSettings.hpp"
|
||||||
#include "settings/QuickRefactorSettings.hpp"
|
#include "settings/QuickRefactorSettings.hpp"
|
||||||
#include "settings/GeneralSettings.hpp"
|
#include "settings/GeneralSettings.hpp"
|
||||||
#include "settings/ProviderSettings.hpp"
|
#include "settings/ProviderSettings.hpp"
|
||||||
|
#include "tools/ToolsRegistration.hpp"
|
||||||
|
|
||||||
namespace QodeAssist::Providers {
|
namespace QodeAssist::Providers {
|
||||||
|
|
||||||
ClaudeProvider::ClaudeProvider(QObject *parent)
|
ClaudeProvider::ClaudeProvider(QObject *parent)
|
||||||
: LLMCore::Provider(parent)
|
: PluginLLMCore::Provider(parent)
|
||||||
, m_toolsManager(new Tools::ToolsManager(this))
|
, m_client(new ::LLMCore::ClaudeClient(QString(), QString(), QString(), this))
|
||||||
{
|
{
|
||||||
connect(
|
Tools::registerQodeAssistTools(m_client->tools());
|
||||||
m_toolsManager,
|
|
||||||
&Tools::ToolsManager::toolExecutionComplete,
|
|
||||||
this,
|
|
||||||
&ClaudeProvider::onToolExecutionComplete);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
QString ClaudeProvider::name() const
|
QString ClaudeProvider::name() const
|
||||||
@@ -50,6 +47,11 @@ QString ClaudeProvider::name() const
|
|||||||
return "Claude";
|
return "Claude";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QString ClaudeProvider::apiKey() const
|
||||||
|
{
|
||||||
|
return Settings::providerSettings().claudeApiKey();
|
||||||
|
}
|
||||||
|
|
||||||
QString ClaudeProvider::url() const
|
QString ClaudeProvider::url() const
|
||||||
{
|
{
|
||||||
return "https://api.anthropic.com";
|
return "https://api.anthropic.com";
|
||||||
@@ -65,16 +67,11 @@ QString ClaudeProvider::chatEndpoint() const
|
|||||||
return "/v1/messages";
|
return "/v1/messages";
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ClaudeProvider::supportsModelListing() const
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
void ClaudeProvider::prepareRequest(
|
void ClaudeProvider::prepareRequest(
|
||||||
QJsonObject &request,
|
QJsonObject &request,
|
||||||
LLMCore::PromptTemplate *prompt,
|
PluginLLMCore::PromptTemplate *prompt,
|
||||||
LLMCore::ContextData context,
|
PluginLLMCore::ContextData context,
|
||||||
LLMCore::RequestType type,
|
PluginLLMCore::RequestType type,
|
||||||
bool isToolsEnabled,
|
bool isToolsEnabled,
|
||||||
bool isThinkingEnabled)
|
bool isThinkingEnabled)
|
||||||
{
|
{
|
||||||
@@ -102,10 +99,10 @@ void ClaudeProvider::prepareRequest(
|
|||||||
request["temperature"] = 1.0;
|
request["temperature"] = 1.0;
|
||||||
};
|
};
|
||||||
|
|
||||||
if (type == LLMCore::RequestType::CodeCompletion) {
|
if (type == PluginLLMCore::RequestType::CodeCompletion) {
|
||||||
applyModelParams(Settings::codeCompletionSettings());
|
applyModelParams(Settings::codeCompletionSettings());
|
||||||
request["temperature"] = Settings::codeCompletionSettings().temperature();
|
request["temperature"] = Settings::codeCompletionSettings().temperature();
|
||||||
} else if (type == LLMCore::RequestType::QuickRefactoring) {
|
} else if (type == PluginLLMCore::RequestType::QuickRefactoring) {
|
||||||
const auto &qrSettings = Settings::quickRefactorSettings();
|
const auto &qrSettings = Settings::quickRefactorSettings();
|
||||||
applyModelParams(qrSettings);
|
applyModelParams(qrSettings);
|
||||||
|
|
||||||
@@ -126,13 +123,8 @@ void ClaudeProvider::prepareRequest(
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (isToolsEnabled) {
|
if (isToolsEnabled) {
|
||||||
LLMCore::RunToolsFilter filter = LLMCore::RunToolsFilter::ALL;
|
auto toolsDefinitions = m_client->tools()->getToolsDefinitions();
|
||||||
if (type == LLMCore::RequestType::QuickRefactoring) {
|
|
||||||
filter = LLMCore::RunToolsFilter::OnlyRead;
|
|
||||||
}
|
|
||||||
|
|
||||||
auto toolsDefinitions = m_toolsManager->getToolsDefinitions(
|
|
||||||
LLMCore::ToolSchemaFormat::Claude, filter);
|
|
||||||
if (!toolsDefinitions.isEmpty()) {
|
if (!toolsDefinitions.isEmpty()) {
|
||||||
request["tools"] = toolsDefinitions;
|
request["tools"] = toolsDefinitions;
|
||||||
LOG_MESSAGE(QString("Added %1 tools to Claude request").arg(toolsDefinitions.size()));
|
LOG_MESSAGE(QString("Added %1 tools to Claude request").arg(toolsDefinitions.size()));
|
||||||
@@ -142,407 +134,26 @@ void ClaudeProvider::prepareRequest(
|
|||||||
|
|
||||||
QFuture<QList<QString>> ClaudeProvider::getInstalledModels(const QString &baseUrl)
|
QFuture<QList<QString>> ClaudeProvider::getInstalledModels(const QString &baseUrl)
|
||||||
{
|
{
|
||||||
QUrl url(baseUrl + "/v1/models");
|
m_client->setUrl(baseUrl);
|
||||||
QUrlQuery query;
|
m_client->setApiKey(apiKey());
|
||||||
query.addQueryItem("limit", "1000");
|
return m_client->listModels();
|
||||||
url.setQuery(query);
|
|
||||||
|
|
||||||
QNetworkRequest request(url);
|
|
||||||
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
|
|
||||||
request.setRawHeader("anthropic-version", "2023-06-01");
|
|
||||||
|
|
||||||
if (!apiKey().isEmpty()) {
|
|
||||||
request.setRawHeader("x-api-key", apiKey().toUtf8());
|
|
||||||
}
|
|
||||||
|
|
||||||
return httpClient()->get(request).then([](const QByteArray &data) {
|
|
||||||
QList<QString> models;
|
|
||||||
QJsonObject jsonObject = QJsonDocument::fromJson(data).object();
|
|
||||||
|
|
||||||
if (jsonObject.contains("data")) {
|
|
||||||
QJsonArray modelArray = jsonObject["data"].toArray();
|
|
||||||
for (const QJsonValue &value : modelArray) {
|
|
||||||
QJsonObject modelObject = value.toObject();
|
|
||||||
if (modelObject.contains("id")) {
|
|
||||||
models.append(modelObject["id"].toString());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return models;
|
|
||||||
}).onFailed([](const std::exception &e) {
|
|
||||||
LOG_MESSAGE(QString("Error fetching Claude models: %1").arg(e.what()));
|
|
||||||
return QList<QString>{};
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
QList<QString> ClaudeProvider::validateRequest(const QJsonObject &request, LLMCore::TemplateType type)
|
PluginLLMCore::ProviderID ClaudeProvider::providerID() const
|
||||||
{
|
{
|
||||||
const auto templateReq = QJsonObject{
|
return PluginLLMCore::ProviderID::Claude;
|
||||||
{"model", {}},
|
|
||||||
{"system", {}},
|
|
||||||
{"messages", QJsonArray{{QJsonObject{{"role", {}}, {"content", {}}}}}},
|
|
||||||
{"temperature", {}},
|
|
||||||
{"max_tokens", {}},
|
|
||||||
{"anthropic-version", {}},
|
|
||||||
{"top_p", {}},
|
|
||||||
{"top_k", {}},
|
|
||||||
{"stop", QJsonArray{}},
|
|
||||||
{"stream", {}},
|
|
||||||
{"tools", {}},
|
|
||||||
{"thinking", QJsonObject{{"type", {}}, {"budget_tokens", {}}}}};
|
|
||||||
|
|
||||||
return LLMCore::ValidationUtils::validateRequestFields(request, templateReq);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
QString ClaudeProvider::apiKey() const
|
PluginLLMCore::ProviderCapabilities ClaudeProvider::capabilities() const
|
||||||
{
|
{
|
||||||
return Settings::providerSettings().claudeApiKey();
|
return PluginLLMCore::ProviderCapability::Tools | PluginLLMCore::ProviderCapability::Thinking
|
||||||
|
| PluginLLMCore::ProviderCapability::Image
|
||||||
|
| PluginLLMCore::ProviderCapability::ModelListing;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ClaudeProvider::prepareNetworkRequest(QNetworkRequest &networkRequest) const
|
::LLMCore::BaseClient *ClaudeProvider::client() const
|
||||||
{
|
{
|
||||||
networkRequest.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
|
return m_client;
|
||||||
networkRequest.setRawHeader("anthropic-version", "2023-06-01");
|
|
||||||
|
|
||||||
if (!apiKey().isEmpty()) {
|
|
||||||
networkRequest.setRawHeader("x-api-key", apiKey().toUtf8());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
LLMCore::ProviderID ClaudeProvider::providerID() const
|
|
||||||
{
|
|
||||||
return LLMCore::ProviderID::Claude;
|
|
||||||
}
|
|
||||||
|
|
||||||
void ClaudeProvider::sendRequest(
|
|
||||||
const LLMCore::RequestID &requestId, const QUrl &url, const QJsonObject &payload)
|
|
||||||
{
|
|
||||||
if (!m_messages.contains(requestId)) {
|
|
||||||
m_dataBuffers[requestId].clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
m_requestUrls[requestId] = url;
|
|
||||||
m_originalRequests[requestId] = payload;
|
|
||||||
|
|
||||||
QNetworkRequest networkRequest(url);
|
|
||||||
prepareNetworkRequest(networkRequest);
|
|
||||||
|
|
||||||
LOG_MESSAGE(QString("ClaudeProvider: Sending request %1 to %2").arg(requestId, url.toString()));
|
|
||||||
|
|
||||||
httpClient()->postStreaming(requestId, networkRequest, payload);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool ClaudeProvider::supportsTools() const
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool ClaudeProvider::supportThinking() const {
|
|
||||||
return true;
|
|
||||||
};
|
|
||||||
|
|
||||||
bool ClaudeProvider::supportImage() const {
|
|
||||||
return true;
|
|
||||||
};
|
|
||||||
|
|
||||||
void ClaudeProvider::cancelRequest(const LLMCore::RequestID &requestId)
|
|
||||||
{
|
|
||||||
LOG_MESSAGE(QString("ClaudeProvider: Cancelling request %1").arg(requestId));
|
|
||||||
LLMCore::Provider::cancelRequest(requestId);
|
|
||||||
cleanupRequest(requestId);
|
|
||||||
}
|
|
||||||
|
|
||||||
LLMCore::IToolsManager *ClaudeProvider::toolsManager() const
|
|
||||||
{
|
|
||||||
return m_toolsManager;
|
|
||||||
}
|
|
||||||
|
|
||||||
void ClaudeProvider::onDataReceived(
|
|
||||||
const QodeAssist::LLMCore::RequestID &requestId, const QByteArray &data)
|
|
||||||
{
|
|
||||||
LLMCore::DataBuffers &buffers = m_dataBuffers[requestId];
|
|
||||||
QStringList lines = buffers.rawStreamBuffer.processData(data);
|
|
||||||
|
|
||||||
for (const QString &line : lines) {
|
|
||||||
QJsonObject responseObj = parseEventLine(line);
|
|
||||||
if (responseObj.isEmpty())
|
|
||||||
continue;
|
|
||||||
|
|
||||||
processStreamEvent(requestId, responseObj);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void ClaudeProvider::onRequestFinished(
|
|
||||||
const QodeAssist::LLMCore::RequestID &requestId, std::optional<QString> error)
|
|
||||||
{
|
|
||||||
if (error) {
|
|
||||||
LOG_MESSAGE(QString("ClaudeProvider request %1 failed: %2").arg(requestId, *error));
|
|
||||||
emit requestFailed(requestId, *error);
|
|
||||||
cleanupRequest(requestId);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (m_messages.contains(requestId)) {
|
|
||||||
ClaudeMessage *message = m_messages[requestId];
|
|
||||||
if (message->state() == LLMCore::MessageState::RequiresToolExecution) {
|
|
||||||
LOG_MESSAGE(QString("Waiting for tools to complete for %1").arg(requestId));
|
|
||||||
m_dataBuffers.remove(requestId);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (m_dataBuffers.contains(requestId)) {
|
|
||||||
const LLMCore::DataBuffers &buffers = m_dataBuffers[requestId];
|
|
||||||
if (!buffers.responseContent.isEmpty()) {
|
|
||||||
LOG_MESSAGE(QString("Emitting full response for %1").arg(requestId));
|
|
||||||
emit fullResponseReceived(requestId, buffers.responseContent);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
cleanupRequest(requestId);
|
|
||||||
}
|
|
||||||
|
|
||||||
void ClaudeProvider::onToolExecutionComplete(
|
|
||||||
const QString &requestId, const QHash<QString, QString> &toolResults)
|
|
||||||
{
|
|
||||||
if (!m_messages.contains(requestId) || !m_requestUrls.contains(requestId)) {
|
|
||||||
LOG_MESSAGE(QString("ERROR: Missing data for continuation request %1").arg(requestId));
|
|
||||||
cleanupRequest(requestId);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
LOG_MESSAGE(QString("Tool execution complete for Claude request %1").arg(requestId));
|
|
||||||
|
|
||||||
for (auto it = toolResults.begin(); it != toolResults.end(); ++it) {
|
|
||||||
ClaudeMessage *message = m_messages[requestId];
|
|
||||||
auto toolContent = message->getCurrentToolUseContent();
|
|
||||||
for (auto tool : toolContent) {
|
|
||||||
if (tool->id() == it.key()) {
|
|
||||||
auto toolStringName = m_toolsManager->toolsFactory()->getStringName(tool->name());
|
|
||||||
emit toolExecutionCompleted(
|
|
||||||
requestId, tool->id(), toolStringName, toolResults[tool->id()]);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
ClaudeMessage *message = m_messages[requestId];
|
|
||||||
QJsonObject continuationRequest = m_originalRequests[requestId];
|
|
||||||
QJsonArray messages = continuationRequest["messages"].toArray();
|
|
||||||
|
|
||||||
messages.append(message->toProviderFormat());
|
|
||||||
|
|
||||||
QJsonObject userMessage;
|
|
||||||
userMessage["role"] = "user";
|
|
||||||
userMessage["content"] = message->createToolResultsContent(toolResults);
|
|
||||||
messages.append(userMessage);
|
|
||||||
|
|
||||||
continuationRequest["messages"] = messages;
|
|
||||||
|
|
||||||
if (continuationRequest.contains("thinking")) {
|
|
||||||
QJsonObject thinkingObj = continuationRequest["thinking"].toObject();
|
|
||||||
LOG_MESSAGE(QString("Thinking mode preserved for continuation: type=%1, budget=%2 tokens")
|
|
||||||
.arg(thinkingObj["type"].toString())
|
|
||||||
.arg(thinkingObj["budget_tokens"].toInt()));
|
|
||||||
}
|
|
||||||
|
|
||||||
LOG_MESSAGE(QString("Sending continuation request for %1 with %2 tool results")
|
|
||||||
.arg(requestId)
|
|
||||||
.arg(toolResults.size()));
|
|
||||||
|
|
||||||
sendRequest(requestId, m_requestUrls[requestId], continuationRequest);
|
|
||||||
}
|
|
||||||
|
|
||||||
void ClaudeProvider::processStreamEvent(const QString &requestId, const QJsonObject &event)
|
|
||||||
{
|
|
||||||
QString eventType = event["type"].toString();
|
|
||||||
|
|
||||||
if (eventType == "message_stop") {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
ClaudeMessage *message = m_messages.value(requestId);
|
|
||||||
if (!message) {
|
|
||||||
if (eventType == "message_start") {
|
|
||||||
message = new ClaudeMessage(this);
|
|
||||||
m_messages[requestId] = message;
|
|
||||||
LOG_MESSAGE(QString("Created NEW ClaudeMessage for request %1").arg(requestId));
|
|
||||||
} else {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (eventType == "message_start") {
|
|
||||||
message->startNewContinuation();
|
|
||||||
emit continuationStarted(requestId);
|
|
||||||
LOG_MESSAGE(QString("Starting NEW continuation for request %1").arg(requestId));
|
|
||||||
|
|
||||||
} else if (eventType == "content_block_start") {
|
|
||||||
int index = event["index"].toInt();
|
|
||||||
QJsonObject contentBlock = event["content_block"].toObject();
|
|
||||||
QString blockType = contentBlock["type"].toString();
|
|
||||||
|
|
||||||
LOG_MESSAGE(
|
|
||||||
QString("Adding new content block: type=%1, index=%2").arg(blockType).arg(index));
|
|
||||||
|
|
||||||
if (blockType == "thinking" || blockType == "redacted_thinking") {
|
|
||||||
QJsonDocument eventDoc(event);
|
|
||||||
LOG_MESSAGE(QString("content_block_start event for %1: %2")
|
|
||||||
.arg(blockType)
|
|
||||||
.arg(QString::fromUtf8(eventDoc.toJson(QJsonDocument::Compact))));
|
|
||||||
}
|
|
||||||
|
|
||||||
message->handleContentBlockStart(index, blockType, contentBlock);
|
|
||||||
|
|
||||||
} else if (eventType == "content_block_delta") {
|
|
||||||
int index = event["index"].toInt();
|
|
||||||
QJsonObject delta = event["delta"].toObject();
|
|
||||||
QString deltaType = delta["type"].toString();
|
|
||||||
|
|
||||||
message->handleContentBlockDelta(index, deltaType, delta);
|
|
||||||
|
|
||||||
if (deltaType == "text_delta") {
|
|
||||||
QString text = delta["text"].toString();
|
|
||||||
LLMCore::DataBuffers &buffers = m_dataBuffers[requestId];
|
|
||||||
buffers.responseContent += text;
|
|
||||||
emit partialResponseReceived(requestId, text);
|
|
||||||
} else if (deltaType == "signature_delta") {
|
|
||||||
QString signature = delta["signature"].toString();
|
|
||||||
}
|
|
||||||
|
|
||||||
} else if (eventType == "content_block_stop") {
|
|
||||||
int index = event["index"].toInt();
|
|
||||||
|
|
||||||
auto allBlocks = message->getCurrentBlocks();
|
|
||||||
if (index < allBlocks.size()) {
|
|
||||||
QString blockType = allBlocks[index]->type();
|
|
||||||
if (blockType == "thinking" || blockType == "redacted_thinking") {
|
|
||||||
QJsonDocument eventDoc(event);
|
|
||||||
LOG_MESSAGE(QString("content_block_stop event for %1 at index %2: %3")
|
|
||||||
.arg(blockType)
|
|
||||||
.arg(index)
|
|
||||||
.arg(QString::fromUtf8(eventDoc.toJson(QJsonDocument::Compact))));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (event.contains("content_block")) {
|
|
||||||
QJsonObject contentBlock = event["content_block"].toObject();
|
|
||||||
QString blockType = contentBlock["type"].toString();
|
|
||||||
|
|
||||||
if (blockType == "thinking") {
|
|
||||||
QString signature = contentBlock["signature"].toString();
|
|
||||||
if (!signature.isEmpty()) {
|
|
||||||
auto allBlocks = message->getCurrentBlocks();
|
|
||||||
if (index < allBlocks.size()) {
|
|
||||||
if (auto thinkingContent = qobject_cast<LLMCore::ThinkingContent *>(allBlocks[index])) {
|
|
||||||
thinkingContent->setSignature(signature);
|
|
||||||
LOG_MESSAGE(
|
|
||||||
QString("Updated thinking block signature from content_block_stop, "
|
|
||||||
"signature length=%1")
|
|
||||||
.arg(signature.length()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else if (blockType == "redacted_thinking") {
|
|
||||||
QString signature = contentBlock["signature"].toString();
|
|
||||||
if (!signature.isEmpty()) {
|
|
||||||
auto allBlocks = message->getCurrentBlocks();
|
|
||||||
if (index < allBlocks.size()) {
|
|
||||||
if (auto redactedContent = qobject_cast<LLMCore::RedactedThinkingContent *>(allBlocks[index])) {
|
|
||||||
redactedContent->setSignature(signature);
|
|
||||||
LOG_MESSAGE(
|
|
||||||
QString("Updated redacted_thinking block signature from content_block_stop, "
|
|
||||||
"signature length=%1")
|
|
||||||
.arg(signature.length()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
message->handleContentBlockStop(index);
|
|
||||||
|
|
||||||
auto thinkingBlocks = message->getCurrentThinkingContent();
|
|
||||||
for (auto thinkingContent : thinkingBlocks) {
|
|
||||||
auto allBlocks = message->getCurrentBlocks();
|
|
||||||
if (index < allBlocks.size() && allBlocks[index] == thinkingContent) {
|
|
||||||
emit thinkingBlockReceived(
|
|
||||||
requestId, thinkingContent->thinking(), thinkingContent->signature());
|
|
||||||
LOG_MESSAGE(
|
|
||||||
QString("Emitted thinking block for request %1, thinking length=%2, signature length=%3")
|
|
||||||
.arg(requestId)
|
|
||||||
.arg(thinkingContent->thinking().length())
|
|
||||||
.arg(thinkingContent->signature().length()));
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
auto redactedBlocks = message->getCurrentRedactedThinkingContent();
|
|
||||||
for (auto redactedContent : redactedBlocks) {
|
|
||||||
auto allBlocks = message->getCurrentBlocks();
|
|
||||||
if (index < allBlocks.size() && allBlocks[index] == redactedContent) {
|
|
||||||
emit redactedThinkingBlockReceived(requestId, redactedContent->signature());
|
|
||||||
LOG_MESSAGE(
|
|
||||||
QString("Emitted redacted thinking block for request %1, signature length=%2")
|
|
||||||
.arg(requestId)
|
|
||||||
.arg(redactedContent->signature().length()));
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
} else if (eventType == "message_delta") {
|
|
||||||
QJsonObject delta = event["delta"].toObject();
|
|
||||||
if (delta.contains("stop_reason")) {
|
|
||||||
QString stopReason = delta["stop_reason"].toString();
|
|
||||||
message->handleStopReason(stopReason);
|
|
||||||
handleMessageComplete(requestId);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void ClaudeProvider::handleMessageComplete(const QString &requestId)
|
|
||||||
{
|
|
||||||
if (!m_messages.contains(requestId))
|
|
||||||
return;
|
|
||||||
|
|
||||||
ClaudeMessage *message = m_messages[requestId];
|
|
||||||
|
|
||||||
if (message->state() == LLMCore::MessageState::RequiresToolExecution) {
|
|
||||||
LOG_MESSAGE(QString("Claude message requires tool execution for %1").arg(requestId));
|
|
||||||
|
|
||||||
auto toolUseContent = message->getCurrentToolUseContent();
|
|
||||||
|
|
||||||
if (toolUseContent.isEmpty()) {
|
|
||||||
LOG_MESSAGE(QString("No tools to execute for %1").arg(requestId));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (auto toolContent : toolUseContent) {
|
|
||||||
auto toolStringName = m_toolsManager->toolsFactory()->getStringName(toolContent->name());
|
|
||||||
emit toolExecutionStarted(requestId, toolContent->id(), toolStringName);
|
|
||||||
|
|
||||||
m_toolsManager->executeToolCall(
|
|
||||||
requestId, toolContent->id(), toolContent->name(), toolContent->input());
|
|
||||||
}
|
|
||||||
|
|
||||||
} else {
|
|
||||||
LOG_MESSAGE(QString("Claude message marked as complete for %1").arg(requestId));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void ClaudeProvider::cleanupRequest(const LLMCore::RequestID &requestId)
|
|
||||||
{
|
|
||||||
LOG_MESSAGE(QString("Cleaning up Claude request %1").arg(requestId));
|
|
||||||
|
|
||||||
if (m_messages.contains(requestId)) {
|
|
||||||
ClaudeMessage *message = m_messages.take(requestId);
|
|
||||||
message->deleteLater();
|
|
||||||
}
|
|
||||||
|
|
||||||
m_dataBuffers.remove(requestId);
|
|
||||||
m_requestUrls.remove(requestId);
|
|
||||||
m_originalRequests.remove(requestId);
|
|
||||||
m_toolsManager->cleanupRequest(requestId);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace QodeAssist::Providers
|
} // namespace QodeAssist::Providers
|
||||||
|
|||||||
@@ -19,14 +19,13 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <llmcore/Provider.hpp>
|
#include <pluginllmcore/Provider.hpp>
|
||||||
|
|
||||||
#include "ClaudeMessage.hpp"
|
#include <LLMCore/ClaudeClient.hpp>
|
||||||
#include "tools/ToolsManager.hpp"
|
|
||||||
|
|
||||||
namespace QodeAssist::Providers {
|
namespace QodeAssist::Providers {
|
||||||
|
|
||||||
class ClaudeProvider : public LLMCore::Provider
|
class ClaudeProvider : public PluginLLMCore::Provider
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
public:
|
public:
|
||||||
@@ -36,50 +35,22 @@ public:
|
|||||||
QString url() const override;
|
QString url() const override;
|
||||||
QString completionEndpoint() const override;
|
QString completionEndpoint() const override;
|
||||||
QString chatEndpoint() const override;
|
QString chatEndpoint() const override;
|
||||||
bool supportsModelListing() const override;
|
|
||||||
void prepareRequest(
|
void prepareRequest(
|
||||||
QJsonObject &request,
|
QJsonObject &request,
|
||||||
LLMCore::PromptTemplate *prompt,
|
PluginLLMCore::PromptTemplate *prompt,
|
||||||
LLMCore::ContextData context,
|
PluginLLMCore::ContextData context,
|
||||||
LLMCore::RequestType type,
|
PluginLLMCore::RequestType type,
|
||||||
bool isToolsEnabled,
|
bool isToolsEnabled,
|
||||||
bool isThinkingEnabled) override;
|
bool isThinkingEnabled) override;
|
||||||
QFuture<QList<QString>> getInstalledModels(const QString &url) override;
|
QFuture<QList<QString>> getInstalledModels(const QString &url) override;
|
||||||
QList<QString> validateRequest(const QJsonObject &request, LLMCore::TemplateType type) override;
|
PluginLLMCore::ProviderID providerID() const override;
|
||||||
|
PluginLLMCore::ProviderCapabilities capabilities() const override;
|
||||||
|
|
||||||
|
::LLMCore::BaseClient *client() const override;
|
||||||
QString apiKey() const override;
|
QString apiKey() const override;
|
||||||
void prepareNetworkRequest(QNetworkRequest &networkRequest) const override;
|
|
||||||
LLMCore::ProviderID providerID() const override;
|
|
||||||
|
|
||||||
void sendRequest(
|
|
||||||
const LLMCore::RequestID &requestId, const QUrl &url, const QJsonObject &payload) override;
|
|
||||||
|
|
||||||
bool supportsTools() const override;
|
|
||||||
bool supportThinking() const override;
|
|
||||||
bool supportImage() const override;
|
|
||||||
void cancelRequest(const LLMCore::RequestID &requestId) override;
|
|
||||||
|
|
||||||
LLMCore::IToolsManager *toolsManager() const override;
|
|
||||||
|
|
||||||
public slots:
|
|
||||||
void onDataReceived(
|
|
||||||
const QodeAssist::LLMCore::RequestID &requestId, const QByteArray &data) override;
|
|
||||||
void onRequestFinished(
|
|
||||||
const QodeAssist::LLMCore::RequestID &requestId,
|
|
||||||
std::optional<QString> error) override;
|
|
||||||
|
|
||||||
private slots:
|
|
||||||
void onToolExecutionComplete(
|
|
||||||
const QString &requestId, const QHash<QString, QString> &toolResults);
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void processStreamEvent(const QString &requestId, const QJsonObject &event);
|
::LLMCore::ClaudeClient *m_client;
|
||||||
void handleMessageComplete(const QString &requestId);
|
|
||||||
void cleanupRequest(const LLMCore::RequestID &requestId);
|
|
||||||
|
|
||||||
QHash<QodeAssist::LLMCore::RequestID, ClaudeMessage *> m_messages;
|
|
||||||
QHash<QodeAssist::LLMCore::RequestID, QUrl> m_requestUrls;
|
|
||||||
QHash<QodeAssist::LLMCore::RequestID, QJsonObject> m_originalRequests;
|
|
||||||
Tools::ToolsManager *m_toolsManager;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace QodeAssist::Providers
|
} // namespace QodeAssist::Providers
|
||||||
|
|||||||
@@ -23,24 +23,28 @@
|
|||||||
|
|
||||||
namespace QodeAssist::Providers {
|
namespace QodeAssist::Providers {
|
||||||
|
|
||||||
|
CodestralProvider::CodestralProvider(QObject *parent)
|
||||||
|
: MistralAIProvider(parent)
|
||||||
|
{}
|
||||||
|
|
||||||
QString CodestralProvider::name() const
|
QString CodestralProvider::name() const
|
||||||
{
|
{
|
||||||
return "Codestral";
|
return "Codestral";
|
||||||
}
|
}
|
||||||
|
|
||||||
QString CodestralProvider::url() const
|
|
||||||
{
|
|
||||||
return "https://codestral.mistral.ai";
|
|
||||||
}
|
|
||||||
|
|
||||||
bool CodestralProvider::supportsModelListing() const
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
QString CodestralProvider::apiKey() const
|
QString CodestralProvider::apiKey() const
|
||||||
{
|
{
|
||||||
return Settings::providerSettings().codestralApiKey();
|
return Settings::providerSettings().codestralApiKey();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QString CodestralProvider::url() const
|
||||||
|
{
|
||||||
|
return "https://codestral.mistral.ai";
|
||||||
|
}
|
||||||
|
|
||||||
|
PluginLLMCore::ProviderCapabilities CodestralProvider::capabilities() const
|
||||||
|
{
|
||||||
|
return PluginLLMCore::ProviderCapability::Tools | PluginLLMCore::ProviderCapability::Image;
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace QodeAssist::Providers
|
} // namespace QodeAssist::Providers
|
||||||
|
|||||||
@@ -26,10 +26,12 @@ namespace QodeAssist::Providers {
|
|||||||
class CodestralProvider : public MistralAIProvider
|
class CodestralProvider : public MistralAIProvider
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
explicit CodestralProvider(QObject *parent = nullptr);
|
||||||
|
|
||||||
QString name() const override;
|
QString name() const override;
|
||||||
QString url() const override;
|
QString url() const override;
|
||||||
bool supportsModelListing() const override;
|
|
||||||
QString apiKey() const override;
|
QString apiKey() const override;
|
||||||
|
PluginLLMCore::ProviderCapabilities capabilities() const override;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace QodeAssist::Providers
|
} // namespace QodeAssist::Providers
|
||||||
|
|||||||
@@ -19,12 +19,14 @@
|
|||||||
|
|
||||||
#include "GoogleAIProvider.hpp"
|
#include "GoogleAIProvider.hpp"
|
||||||
|
|
||||||
|
#include <LLMCore/ToolsManager.hpp>
|
||||||
|
|
||||||
#include <QJsonArray>
|
#include <QJsonArray>
|
||||||
|
#include "tools/ToolsRegistration.hpp"
|
||||||
#include <QJsonDocument>
|
#include <QJsonDocument>
|
||||||
#include <QJsonObject>
|
#include <QJsonObject>
|
||||||
#include <QtCore/qurlquery.h>
|
#include <QtCore/qurlquery.h>
|
||||||
|
|
||||||
#include "llmcore/ValidationUtils.hpp"
|
|
||||||
#include "logger/Logger.hpp"
|
#include "logger/Logger.hpp"
|
||||||
#include "settings/ChatAssistantSettings.hpp"
|
#include "settings/ChatAssistantSettings.hpp"
|
||||||
#include "settings/CodeCompletionSettings.hpp"
|
#include "settings/CodeCompletionSettings.hpp"
|
||||||
@@ -35,14 +37,10 @@
|
|||||||
namespace QodeAssist::Providers {
|
namespace QodeAssist::Providers {
|
||||||
|
|
||||||
GoogleAIProvider::GoogleAIProvider(QObject *parent)
|
GoogleAIProvider::GoogleAIProvider(QObject *parent)
|
||||||
: LLMCore::Provider(parent)
|
: PluginLLMCore::Provider(parent)
|
||||||
, m_toolsManager(new Tools::ToolsManager(this))
|
, m_client(new ::LLMCore::GoogleAIClient(QString(), QString(), QString(), this))
|
||||||
{
|
{
|
||||||
connect(
|
Tools::registerQodeAssistTools(m_client->tools());
|
||||||
m_toolsManager,
|
|
||||||
&Tools::ToolsManager::toolExecutionComplete,
|
|
||||||
this,
|
|
||||||
&GoogleAIProvider::onToolExecutionComplete);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
QString GoogleAIProvider::name() const
|
QString GoogleAIProvider::name() const
|
||||||
@@ -50,6 +48,11 @@ QString GoogleAIProvider::name() const
|
|||||||
return "Google AI";
|
return "Google AI";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QString GoogleAIProvider::apiKey() const
|
||||||
|
{
|
||||||
|
return Settings::providerSettings().googleAiApiKey();
|
||||||
|
}
|
||||||
|
|
||||||
QString GoogleAIProvider::url() const
|
QString GoogleAIProvider::url() const
|
||||||
{
|
{
|
||||||
return "https://generativelanguage.googleapis.com/v1beta";
|
return "https://generativelanguage.googleapis.com/v1beta";
|
||||||
@@ -65,16 +68,11 @@ QString GoogleAIProvider::chatEndpoint() const
|
|||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
bool GoogleAIProvider::supportsModelListing() const
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
void GoogleAIProvider::prepareRequest(
|
void GoogleAIProvider::prepareRequest(
|
||||||
QJsonObject &request,
|
QJsonObject &request,
|
||||||
LLMCore::PromptTemplate *prompt,
|
PluginLLMCore::PromptTemplate *prompt,
|
||||||
LLMCore::ContextData context,
|
PluginLLMCore::ContextData context,
|
||||||
LLMCore::RequestType type,
|
PluginLLMCore::RequestType type,
|
||||||
bool isToolsEnabled,
|
bool isToolsEnabled,
|
||||||
bool isThinkingEnabled)
|
bool isThinkingEnabled)
|
||||||
{
|
{
|
||||||
@@ -119,9 +117,9 @@ void GoogleAIProvider::prepareRequest(
|
|||||||
request["generationConfig"] = generationConfig;
|
request["generationConfig"] = generationConfig;
|
||||||
};
|
};
|
||||||
|
|
||||||
if (type == LLMCore::RequestType::CodeCompletion) {
|
if (type == PluginLLMCore::RequestType::CodeCompletion) {
|
||||||
applyModelParams(Settings::codeCompletionSettings());
|
applyModelParams(Settings::codeCompletionSettings());
|
||||||
} else if (type == LLMCore::RequestType::QuickRefactoring) {
|
} else if (type == PluginLLMCore::RequestType::QuickRefactoring) {
|
||||||
const auto &qrSettings = Settings::quickRefactorSettings();
|
const auto &qrSettings = Settings::quickRefactorSettings();
|
||||||
|
|
||||||
if (isThinkingEnabled) {
|
if (isThinkingEnabled) {
|
||||||
@@ -140,13 +138,7 @@ void GoogleAIProvider::prepareRequest(
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (isToolsEnabled) {
|
if (isToolsEnabled) {
|
||||||
LLMCore::RunToolsFilter filter = LLMCore::RunToolsFilter::ALL;
|
auto toolsDefinitions = m_client->tools()->getToolsDefinitions();
|
||||||
if (type == LLMCore::RequestType::QuickRefactoring) {
|
|
||||||
filter = LLMCore::RunToolsFilter::OnlyRead;
|
|
||||||
}
|
|
||||||
|
|
||||||
auto toolsDefinitions = m_toolsManager->getToolsDefinitions(
|
|
||||||
LLMCore::ToolSchemaFormat::Google, filter);
|
|
||||||
if (!toolsDefinitions.isEmpty()) {
|
if (!toolsDefinitions.isEmpty()) {
|
||||||
request["tools"] = toolsDefinitions;
|
request["tools"] = toolsDefinitions;
|
||||||
LOG_MESSAGE(QString("Added %1 tools to Google AI request").arg(toolsDefinitions.size()));
|
LOG_MESSAGE(QString("Added %1 tools to Google AI request").arg(toolsDefinitions.size()));
|
||||||
@@ -154,422 +146,28 @@ void GoogleAIProvider::prepareRequest(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
QFuture<QList<QString>> GoogleAIProvider::getInstalledModels(const QString &url)
|
QFuture<QList<QString>> GoogleAIProvider::getInstalledModels(const QString &baseUrl)
|
||||||
{
|
{
|
||||||
QNetworkRequest request(QString("%1/models?key=%2").arg(url, apiKey()));
|
m_client->setUrl(baseUrl);
|
||||||
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
|
m_client->setApiKey(apiKey());
|
||||||
|
return m_client->listModels();
|
||||||
return httpClient()->get(request).then([](const QByteArray &data) {
|
|
||||||
QList<QString> models;
|
|
||||||
QJsonObject jsonObject = QJsonDocument::fromJson(data).object();
|
|
||||||
|
|
||||||
if (jsonObject.contains("models")) {
|
|
||||||
QJsonArray modelArray = jsonObject["models"].toArray();
|
|
||||||
for (const QJsonValue &value : modelArray) {
|
|
||||||
QJsonObject modelObject = value.toObject();
|
|
||||||
if (modelObject.contains("name")) {
|
|
||||||
QString modelName = modelObject["name"].toString();
|
|
||||||
if (modelName.contains("/")) {
|
|
||||||
modelName = modelName.split("/").last();
|
|
||||||
}
|
|
||||||
models.append(modelName);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return models;
|
|
||||||
}).onFailed([](const std::exception &e) {
|
|
||||||
LOG_MESSAGE(QString("Error fetching Google AI models: %1").arg(e.what()));
|
|
||||||
return QList<QString>{};
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
QList<QString> GoogleAIProvider::validateRequest(
|
PluginLLMCore::ProviderID GoogleAIProvider::providerID() const
|
||||||
const QJsonObject &request, LLMCore::TemplateType type)
|
|
||||||
{
|
{
|
||||||
QJsonObject templateReq;
|
return PluginLLMCore::ProviderID::GoogleAI;
|
||||||
|
|
||||||
templateReq = QJsonObject{
|
|
||||||
{"contents", QJsonArray{}},
|
|
||||||
{"system_instruction", QJsonArray{}},
|
|
||||||
{"generationConfig",
|
|
||||||
QJsonObject{
|
|
||||||
{"temperature", {}},
|
|
||||||
{"maxOutputTokens", {}},
|
|
||||||
{"topP", {}},
|
|
||||||
{"topK", {}},
|
|
||||||
{"thinkingConfig",
|
|
||||||
QJsonObject{{"thinkingBudget", {}}, {"includeThoughts", {}}}}}},
|
|
||||||
{"safetySettings", QJsonArray{}},
|
|
||||||
{"tools", QJsonArray{}}};
|
|
||||||
|
|
||||||
return LLMCore::ValidationUtils::validateRequestFields(request, templateReq);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
QString GoogleAIProvider::apiKey() const
|
PluginLLMCore::ProviderCapabilities GoogleAIProvider::capabilities() const
|
||||||
{
|
{
|
||||||
return Settings::providerSettings().googleAiApiKey();
|
return PluginLLMCore::ProviderCapability::Tools | PluginLLMCore::ProviderCapability::Thinking
|
||||||
|
| PluginLLMCore::ProviderCapability::Image
|
||||||
|
| PluginLLMCore::ProviderCapability::ModelListing;
|
||||||
}
|
}
|
||||||
|
|
||||||
void GoogleAIProvider::prepareNetworkRequest(QNetworkRequest &networkRequest) const
|
::LLMCore::BaseClient *GoogleAIProvider::client() const
|
||||||
{
|
{
|
||||||
networkRequest.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
|
return m_client;
|
||||||
|
|
||||||
QUrl url = networkRequest.url();
|
|
||||||
QUrlQuery query(url.query());
|
|
||||||
query.addQueryItem("key", apiKey());
|
|
||||||
url.setQuery(query);
|
|
||||||
networkRequest.setUrl(url);
|
|
||||||
}
|
|
||||||
|
|
||||||
LLMCore::ProviderID GoogleAIProvider::providerID() const
|
|
||||||
{
|
|
||||||
return LLMCore::ProviderID::GoogleAI;
|
|
||||||
}
|
|
||||||
|
|
||||||
void GoogleAIProvider::sendRequest(
|
|
||||||
const LLMCore::RequestID &requestId, const QUrl &url, const QJsonObject &payload)
|
|
||||||
{
|
|
||||||
if (!m_messages.contains(requestId)) {
|
|
||||||
m_dataBuffers[requestId].clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
m_requestUrls[requestId] = url;
|
|
||||||
m_originalRequests[requestId] = payload;
|
|
||||||
|
|
||||||
QNetworkRequest networkRequest(url);
|
|
||||||
prepareNetworkRequest(networkRequest);
|
|
||||||
|
|
||||||
LOG_MESSAGE(
|
|
||||||
QString("GoogleAIProvider: Sending request %1 to %2").arg(requestId, url.toString()));
|
|
||||||
|
|
||||||
httpClient()->postStreaming(requestId, networkRequest, payload);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool GoogleAIProvider::supportsTools() const
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool GoogleAIProvider::supportThinking() const
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool GoogleAIProvider::supportImage() const
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
void GoogleAIProvider::cancelRequest(const LLMCore::RequestID &requestId)
|
|
||||||
{
|
|
||||||
LOG_MESSAGE(QString("GoogleAIProvider: Cancelling request %1").arg(requestId));
|
|
||||||
LLMCore::Provider::cancelRequest(requestId);
|
|
||||||
cleanupRequest(requestId);
|
|
||||||
}
|
|
||||||
|
|
||||||
void GoogleAIProvider::onDataReceived(
|
|
||||||
const QodeAssist::LLMCore::RequestID &requestId, const QByteArray &data)
|
|
||||||
{
|
|
||||||
if (data.isEmpty()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
QJsonParseError parseError;
|
|
||||||
QJsonDocument doc = QJsonDocument::fromJson(data, &parseError);
|
|
||||||
if (!doc.isNull() && doc.isObject()) {
|
|
||||||
QJsonObject obj = doc.object();
|
|
||||||
if (obj.contains("error")) {
|
|
||||||
QJsonObject error = obj["error"].toObject();
|
|
||||||
QString errorMessage = error["message"].toString();
|
|
||||||
int errorCode = error["code"].toInt();
|
|
||||||
QString fullError
|
|
||||||
= QString("Google AI API Error %1: %2").arg(errorCode).arg(errorMessage);
|
|
||||||
|
|
||||||
LOG_MESSAGE(fullError);
|
|
||||||
emit requestFailed(requestId, fullError);
|
|
||||||
cleanupRequest(requestId);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
LLMCore::DataBuffers &buffers = m_dataBuffers[requestId];
|
|
||||||
QStringList lines = buffers.rawStreamBuffer.processData(data);
|
|
||||||
|
|
||||||
for (const QString &line : lines) {
|
|
||||||
if (line.trimmed().isEmpty()) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
QJsonObject chunk = parseEventLine(line);
|
|
||||||
if (chunk.isEmpty())
|
|
||||||
continue;
|
|
||||||
|
|
||||||
processStreamChunk(requestId, chunk);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void GoogleAIProvider::onRequestFinished(
|
|
||||||
const QodeAssist::LLMCore::RequestID &requestId, std::optional<QString> error)
|
|
||||||
{
|
|
||||||
if (error) {
|
|
||||||
LOG_MESSAGE(QString("GoogleAIProvider request %1 failed: %2").arg(requestId, *error));
|
|
||||||
emit requestFailed(requestId, *error);
|
|
||||||
cleanupRequest(requestId);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (m_failedRequests.contains(requestId)) {
|
|
||||||
cleanupRequest(requestId);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
emitPendingThinkingBlocks(requestId);
|
|
||||||
|
|
||||||
if (m_messages.contains(requestId)) {
|
|
||||||
GoogleMessage *message = m_messages[requestId];
|
|
||||||
|
|
||||||
handleMessageComplete(requestId);
|
|
||||||
|
|
||||||
if (message->state() == LLMCore::MessageState::RequiresToolExecution) {
|
|
||||||
LOG_MESSAGE(QString("Waiting for tools to complete for %1").arg(requestId));
|
|
||||||
m_dataBuffers.remove(requestId);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (m_dataBuffers.contains(requestId)) {
|
|
||||||
const LLMCore::DataBuffers &buffers = m_dataBuffers[requestId];
|
|
||||||
if (!buffers.responseContent.isEmpty()) {
|
|
||||||
emit fullResponseReceived(requestId, buffers.responseContent);
|
|
||||||
} else {
|
|
||||||
emit fullResponseReceived(requestId, QString());
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
emit fullResponseReceived(requestId, QString());
|
|
||||||
}
|
|
||||||
|
|
||||||
cleanupRequest(requestId);
|
|
||||||
}
|
|
||||||
|
|
||||||
void GoogleAIProvider::onToolExecutionComplete(
|
|
||||||
const QString &requestId, const QHash<QString, QString> &toolResults)
|
|
||||||
{
|
|
||||||
if (!m_messages.contains(requestId) || !m_requestUrls.contains(requestId)) {
|
|
||||||
LOG_MESSAGE(QString("ERROR: Missing data for continuation request %1").arg(requestId));
|
|
||||||
cleanupRequest(requestId);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (auto it = toolResults.begin(); it != toolResults.end(); ++it) {
|
|
||||||
GoogleMessage *message = m_messages[requestId];
|
|
||||||
auto toolContent = message->getCurrentToolUseContent();
|
|
||||||
for (auto tool : toolContent) {
|
|
||||||
if (tool->id() == it.key()) {
|
|
||||||
auto toolStringName = m_toolsManager->toolsFactory()->getStringName(tool->name());
|
|
||||||
emit toolExecutionCompleted(
|
|
||||||
requestId, tool->id(), toolStringName, toolResults[tool->id()]);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
GoogleMessage *message = m_messages[requestId];
|
|
||||||
QJsonObject continuationRequest = m_originalRequests[requestId];
|
|
||||||
QJsonArray contents = continuationRequest["contents"].toArray();
|
|
||||||
|
|
||||||
contents.append(message->toProviderFormat());
|
|
||||||
|
|
||||||
QJsonObject userMessage;
|
|
||||||
userMessage["role"] = "user";
|
|
||||||
userMessage["parts"] = message->createToolResultParts(toolResults);
|
|
||||||
contents.append(userMessage);
|
|
||||||
|
|
||||||
continuationRequest["contents"] = contents;
|
|
||||||
|
|
||||||
sendRequest(requestId, m_requestUrls[requestId], continuationRequest);
|
|
||||||
}
|
|
||||||
|
|
||||||
void GoogleAIProvider::processStreamChunk(const QString &requestId, const QJsonObject &chunk)
|
|
||||||
{
|
|
||||||
if (!chunk.contains("candidates")) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
GoogleMessage *message = m_messages.value(requestId);
|
|
||||||
if (!message) {
|
|
||||||
message = new GoogleMessage(this);
|
|
||||||
m_messages[requestId] = message;
|
|
||||||
LOG_MESSAGE(QString("Created NEW GoogleMessage for request %1").arg(requestId));
|
|
||||||
|
|
||||||
if (m_dataBuffers.contains(requestId)) {
|
|
||||||
emit continuationStarted(requestId);
|
|
||||||
LOG_MESSAGE(QString("Starting continuation for request %1").arg(requestId));
|
|
||||||
}
|
|
||||||
} else if (
|
|
||||||
m_dataBuffers.contains(requestId)
|
|
||||||
&& message->state() == LLMCore::MessageState::RequiresToolExecution) {
|
|
||||||
message->startNewContinuation();
|
|
||||||
m_emittedThinkingBlocksCount[requestId] = 0;
|
|
||||||
LOG_MESSAGE(QString("Cleared message state for continuation request %1").arg(requestId));
|
|
||||||
}
|
|
||||||
|
|
||||||
QJsonArray candidates = chunk["candidates"].toArray();
|
|
||||||
for (const QJsonValue &candidate : candidates) {
|
|
||||||
QJsonObject candidateObj = candidate.toObject();
|
|
||||||
|
|
||||||
if (candidateObj.contains("content")) {
|
|
||||||
QJsonObject content = candidateObj["content"].toObject();
|
|
||||||
if (content.contains("parts")) {
|
|
||||||
QJsonArray parts = content["parts"].toArray();
|
|
||||||
for (const QJsonValue &part : parts) {
|
|
||||||
QJsonObject partObj = part.toObject();
|
|
||||||
|
|
||||||
if (partObj.contains("text")) {
|
|
||||||
QString text = partObj["text"].toString();
|
|
||||||
bool isThought = partObj.value("thought").toBool(false);
|
|
||||||
|
|
||||||
if (isThought) {
|
|
||||||
message->handleThoughtDelta(text);
|
|
||||||
|
|
||||||
if (partObj.contains("signature")) {
|
|
||||||
QString signature = partObj["signature"].toString();
|
|
||||||
message->handleThoughtSignature(signature);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
emitPendingThinkingBlocks(requestId);
|
|
||||||
|
|
||||||
message->handleContentDelta(text);
|
|
||||||
|
|
||||||
LLMCore::DataBuffers &buffers = m_dataBuffers[requestId];
|
|
||||||
buffers.responseContent += text;
|
|
||||||
emit partialResponseReceived(requestId, text);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (partObj.contains("thoughtSignature")) {
|
|
||||||
QString signature = partObj["thoughtSignature"].toString();
|
|
||||||
message->handleThoughtSignature(signature);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (partObj.contains("functionCall")) {
|
|
||||||
emitPendingThinkingBlocks(requestId);
|
|
||||||
|
|
||||||
QJsonObject functionCall = partObj["functionCall"].toObject();
|
|
||||||
QString name = functionCall["name"].toString();
|
|
||||||
QJsonObject args = functionCall["args"].toObject();
|
|
||||||
|
|
||||||
message->handleFunctionCallStart(name);
|
|
||||||
message->handleFunctionCallArgsDelta(
|
|
||||||
QString::fromUtf8(QJsonDocument(args).toJson(QJsonDocument::Compact)));
|
|
||||||
message->handleFunctionCallComplete();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (candidateObj.contains("finishReason")) {
|
|
||||||
QString finishReason = candidateObj["finishReason"].toString();
|
|
||||||
message->handleFinishReason(finishReason);
|
|
||||||
|
|
||||||
if (message->isErrorFinishReason()) {
|
|
||||||
QString errorMessage = message->getErrorMessage();
|
|
||||||
LOG_MESSAGE(QString("Google AI error: %1").arg(errorMessage));
|
|
||||||
m_failedRequests.insert(requestId);
|
|
||||||
emit requestFailed(requestId, errorMessage);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (chunk.contains("usageMetadata")) {
|
|
||||||
QJsonObject usageMetadata = chunk["usageMetadata"].toObject();
|
|
||||||
int thoughtsTokenCount = usageMetadata.value("thoughtsTokenCount").toInt(0);
|
|
||||||
int candidatesTokenCount = usageMetadata.value("candidatesTokenCount").toInt(0);
|
|
||||||
int totalTokenCount = usageMetadata.value("totalTokenCount").toInt(0);
|
|
||||||
|
|
||||||
if (totalTokenCount > 0) {
|
|
||||||
LOG_MESSAGE(QString("Google AI tokens: %1 (thoughts: %2, output: %3)")
|
|
||||||
.arg(totalTokenCount)
|
|
||||||
.arg(thoughtsTokenCount)
|
|
||||||
.arg(candidatesTokenCount));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void GoogleAIProvider::emitPendingThinkingBlocks(const QString &requestId)
|
|
||||||
{
|
|
||||||
if (!m_messages.contains(requestId))
|
|
||||||
return;
|
|
||||||
|
|
||||||
GoogleMessage *message = m_messages[requestId];
|
|
||||||
auto thinkingBlocks = message->getCurrentThinkingContent();
|
|
||||||
|
|
||||||
if (thinkingBlocks.isEmpty())
|
|
||||||
return;
|
|
||||||
|
|
||||||
int alreadyEmitted = m_emittedThinkingBlocksCount.value(requestId, 0);
|
|
||||||
int totalBlocks = thinkingBlocks.size();
|
|
||||||
|
|
||||||
for (int i = alreadyEmitted; i < totalBlocks; ++i) {
|
|
||||||
auto thinkingContent = thinkingBlocks[i];
|
|
||||||
|
|
||||||
if (thinkingContent->thinking().trimmed().isEmpty()) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
emit thinkingBlockReceived(
|
|
||||||
requestId,
|
|
||||||
thinkingContent->thinking(),
|
|
||||||
thinkingContent->signature());
|
|
||||||
}
|
|
||||||
|
|
||||||
m_emittedThinkingBlocksCount[requestId] = totalBlocks;
|
|
||||||
}
|
|
||||||
|
|
||||||
void GoogleAIProvider::handleMessageComplete(const QString &requestId)
|
|
||||||
{
|
|
||||||
if (!m_messages.contains(requestId))
|
|
||||||
return;
|
|
||||||
|
|
||||||
GoogleMessage *message = m_messages[requestId];
|
|
||||||
|
|
||||||
if (message->state() == LLMCore::MessageState::RequiresToolExecution) {
|
|
||||||
LOG_MESSAGE(QString("Google AI message requires tool execution for %1").arg(requestId));
|
|
||||||
|
|
||||||
auto toolUseContent = message->getCurrentToolUseContent();
|
|
||||||
|
|
||||||
if (toolUseContent.isEmpty()) {
|
|
||||||
LOG_MESSAGE(QString("No tools to execute for %1").arg(requestId));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (auto toolContent : toolUseContent) {
|
|
||||||
auto toolStringName = m_toolsManager->toolsFactory()->getStringName(toolContent->name());
|
|
||||||
emit toolExecutionStarted(requestId, toolContent->id(), toolStringName);
|
|
||||||
m_toolsManager->executeToolCall(
|
|
||||||
requestId, toolContent->id(), toolContent->name(), toolContent->input());
|
|
||||||
}
|
|
||||||
|
|
||||||
} else {
|
|
||||||
LOG_MESSAGE(QString("Google AI message marked as complete for %1").arg(requestId));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void GoogleAIProvider::cleanupRequest(const LLMCore::RequestID &requestId)
|
|
||||||
{
|
|
||||||
LOG_MESSAGE(QString("Cleaning up Google AI request %1").arg(requestId));
|
|
||||||
|
|
||||||
if (m_messages.contains(requestId)) {
|
|
||||||
GoogleMessage *message = m_messages.take(requestId);
|
|
||||||
message->deleteLater();
|
|
||||||
}
|
|
||||||
|
|
||||||
m_dataBuffers.remove(requestId);
|
|
||||||
m_requestUrls.remove(requestId);
|
|
||||||
m_originalRequests.remove(requestId);
|
|
||||||
m_emittedThinkingBlocksCount.remove(requestId);
|
|
||||||
m_failedRequests.remove(requestId);
|
|
||||||
m_toolsManager->cleanupRequest(requestId);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace QodeAssist::Providers
|
} // namespace QodeAssist::Providers
|
||||||
|
|||||||
@@ -19,13 +19,13 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "GoogleMessage.hpp"
|
#include <pluginllmcore/Provider.hpp>
|
||||||
#include "llmcore/Provider.hpp"
|
|
||||||
#include "tools/ToolsManager.hpp"
|
#include <LLMCore/GoogleAIClient.hpp>
|
||||||
|
|
||||||
namespace QodeAssist::Providers {
|
namespace QodeAssist::Providers {
|
||||||
|
|
||||||
class GoogleAIProvider : public LLMCore::Provider
|
class GoogleAIProvider : public PluginLLMCore::Provider
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
public:
|
public:
|
||||||
@@ -35,51 +35,22 @@ public:
|
|||||||
QString url() const override;
|
QString url() const override;
|
||||||
QString completionEndpoint() const override;
|
QString completionEndpoint() const override;
|
||||||
QString chatEndpoint() const override;
|
QString chatEndpoint() const override;
|
||||||
bool supportsModelListing() const override;
|
|
||||||
void prepareRequest(
|
void prepareRequest(
|
||||||
QJsonObject &request,
|
QJsonObject &request,
|
||||||
LLMCore::PromptTemplate *prompt,
|
PluginLLMCore::PromptTemplate *prompt,
|
||||||
LLMCore::ContextData context,
|
PluginLLMCore::ContextData context,
|
||||||
LLMCore::RequestType type,
|
PluginLLMCore::RequestType type,
|
||||||
bool isToolsEnabled,
|
bool isToolsEnabled,
|
||||||
bool isThinkingEnabled) override;
|
bool isThinkingEnabled) override;
|
||||||
QFuture<QList<QString>> getInstalledModels(const QString &url) override;
|
QFuture<QList<QString>> getInstalledModels(const QString &url) override;
|
||||||
QList<QString> validateRequest(const QJsonObject &request, LLMCore::TemplateType type) override;
|
PluginLLMCore::ProviderID providerID() const override;
|
||||||
|
PluginLLMCore::ProviderCapabilities capabilities() const override;
|
||||||
|
|
||||||
|
::LLMCore::BaseClient *client() const override;
|
||||||
QString apiKey() const override;
|
QString apiKey() const override;
|
||||||
void prepareNetworkRequest(QNetworkRequest &networkRequest) const override;
|
|
||||||
LLMCore::ProviderID providerID() const override;
|
|
||||||
|
|
||||||
void sendRequest(
|
|
||||||
const LLMCore::RequestID &requestId, const QUrl &url, const QJsonObject &payload) override;
|
|
||||||
|
|
||||||
bool supportsTools() const override;
|
|
||||||
bool supportThinking() const override;
|
|
||||||
bool supportImage() const override;
|
|
||||||
void cancelRequest(const LLMCore::RequestID &requestId) override;
|
|
||||||
|
|
||||||
public slots:
|
|
||||||
void onDataReceived(
|
|
||||||
const QodeAssist::LLMCore::RequestID &requestId, const QByteArray &data) override;
|
|
||||||
void onRequestFinished(
|
|
||||||
const QodeAssist::LLMCore::RequestID &requestId,
|
|
||||||
std::optional<QString> error) override;
|
|
||||||
|
|
||||||
private slots:
|
|
||||||
void onToolExecutionComplete(
|
|
||||||
const QString &requestId, const QHash<QString, QString> &toolResults);
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void processStreamChunk(const QString &requestId, const QJsonObject &chunk);
|
::LLMCore::GoogleAIClient *m_client;
|
||||||
void handleMessageComplete(const QString &requestId);
|
|
||||||
void emitPendingThinkingBlocks(const QString &requestId);
|
|
||||||
void cleanupRequest(const LLMCore::RequestID &requestId);
|
|
||||||
|
|
||||||
QHash<LLMCore::RequestID, GoogleMessage *> m_messages;
|
|
||||||
QHash<LLMCore::RequestID, QUrl> m_requestUrls;
|
|
||||||
QHash<LLMCore::RequestID, QJsonObject> m_originalRequests;
|
|
||||||
QHash<LLMCore::RequestID, int> m_emittedThinkingBlocksCount;
|
|
||||||
QSet<LLMCore::RequestID> m_failedRequests;
|
|
||||||
Tools::ToolsManager *m_toolsManager;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace QodeAssist::Providers
|
} // namespace QodeAssist::Providers
|
||||||
|
|||||||
@@ -32,26 +32,26 @@ GoogleMessage::GoogleMessage(QObject *parent)
|
|||||||
|
|
||||||
void GoogleMessage::handleContentDelta(const QString &text)
|
void GoogleMessage::handleContentDelta(const QString &text)
|
||||||
{
|
{
|
||||||
if (m_currentBlocks.isEmpty() || !qobject_cast<LLMCore::TextContent *>(m_currentBlocks.last())) {
|
if (m_currentBlocks.isEmpty() || !qobject_cast<PluginLLMCore::TextContent *>(m_currentBlocks.last())) {
|
||||||
auto textContent = new LLMCore::TextContent();
|
auto textContent = new PluginLLMCore::TextContent();
|
||||||
textContent->setParent(this);
|
textContent->setParent(this);
|
||||||
m_currentBlocks.append(textContent);
|
m_currentBlocks.append(textContent);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (auto textContent = qobject_cast<LLMCore::TextContent *>(m_currentBlocks.last())) {
|
if (auto textContent = qobject_cast<PluginLLMCore::TextContent *>(m_currentBlocks.last())) {
|
||||||
textContent->appendText(text);
|
textContent->appendText(text);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void GoogleMessage::handleThoughtDelta(const QString &text)
|
void GoogleMessage::handleThoughtDelta(const QString &text)
|
||||||
{
|
{
|
||||||
if (m_currentBlocks.isEmpty() || !qobject_cast<LLMCore::ThinkingContent *>(m_currentBlocks.last())) {
|
if (m_currentBlocks.isEmpty() || !qobject_cast<PluginLLMCore::ThinkingContent *>(m_currentBlocks.last())) {
|
||||||
auto thinkingContent = new LLMCore::ThinkingContent();
|
auto thinkingContent = new PluginLLMCore::ThinkingContent();
|
||||||
thinkingContent->setParent(this);
|
thinkingContent->setParent(this);
|
||||||
m_currentBlocks.append(thinkingContent);
|
m_currentBlocks.append(thinkingContent);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (auto thinkingContent = qobject_cast<LLMCore::ThinkingContent *>(m_currentBlocks.last())) {
|
if (auto thinkingContent = qobject_cast<PluginLLMCore::ThinkingContent *>(m_currentBlocks.last())) {
|
||||||
thinkingContent->appendThinking(text);
|
thinkingContent->appendThinking(text);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -59,13 +59,13 @@ void GoogleMessage::handleThoughtDelta(const QString &text)
|
|||||||
void GoogleMessage::handleThoughtSignature(const QString &signature)
|
void GoogleMessage::handleThoughtSignature(const QString &signature)
|
||||||
{
|
{
|
||||||
for (int i = m_currentBlocks.size() - 1; i >= 0; --i) {
|
for (int i = m_currentBlocks.size() - 1; i >= 0; --i) {
|
||||||
if (auto thinkingContent = qobject_cast<LLMCore::ThinkingContent *>(m_currentBlocks[i])) {
|
if (auto thinkingContent = qobject_cast<PluginLLMCore::ThinkingContent *>(m_currentBlocks[i])) {
|
||||||
thinkingContent->setSignature(signature);
|
thinkingContent->setSignature(signature);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
auto thinkingContent = new LLMCore::ThinkingContent();
|
auto thinkingContent = new PluginLLMCore::ThinkingContent();
|
||||||
thinkingContent->setParent(this);
|
thinkingContent->setParent(this);
|
||||||
thinkingContent->setSignature(signature);
|
thinkingContent->setSignature(signature);
|
||||||
m_currentBlocks.append(thinkingContent);
|
m_currentBlocks.append(thinkingContent);
|
||||||
@@ -97,7 +97,7 @@ void GoogleMessage::handleFunctionCallComplete()
|
|||||||
}
|
}
|
||||||
|
|
||||||
QString id = QUuid::createUuid().toString(QUuid::WithoutBraces);
|
QString id = QUuid::createUuid().toString(QUuid::WithoutBraces);
|
||||||
auto toolContent = new LLMCore::ToolUseContent(id, m_currentFunctionName, args);
|
auto toolContent = new PluginLLMCore::ToolUseContent(id, m_currentFunctionName, args);
|
||||||
toolContent->setParent(this);
|
toolContent->setParent(this);
|
||||||
m_currentBlocks.append(toolContent);
|
m_currentBlocks.append(toolContent);
|
||||||
|
|
||||||
@@ -122,14 +122,14 @@ QJsonObject GoogleMessage::toProviderFormat() const
|
|||||||
if (!block)
|
if (!block)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
if (auto text = qobject_cast<LLMCore::TextContent *>(block)) {
|
if (auto text = qobject_cast<PluginLLMCore::TextContent *>(block)) {
|
||||||
parts.append(QJsonObject{{"text", text->text()}});
|
parts.append(QJsonObject{{"text", text->text()}});
|
||||||
} else if (auto tool = qobject_cast<LLMCore::ToolUseContent *>(block)) {
|
} else if (auto tool = qobject_cast<PluginLLMCore::ToolUseContent *>(block)) {
|
||||||
QJsonObject functionCall;
|
QJsonObject functionCall;
|
||||||
functionCall["name"] = tool->name();
|
functionCall["name"] = tool->name();
|
||||||
functionCall["args"] = tool->input();
|
functionCall["args"] = tool->input();
|
||||||
parts.append(QJsonObject{{"functionCall", functionCall}});
|
parts.append(QJsonObject{{"functionCall", functionCall}});
|
||||||
} else if (auto thinking = qobject_cast<LLMCore::ThinkingContent *>(block)) {
|
} else if (auto thinking = qobject_cast<PluginLLMCore::ThinkingContent *>(block)) {
|
||||||
// Include thinking blocks with their text
|
// Include thinking blocks with their text
|
||||||
QJsonObject thinkingPart;
|
QJsonObject thinkingPart;
|
||||||
thinkingPart["text"] = thinking->thinking();
|
thinkingPart["text"] = thinking->thinking();
|
||||||
@@ -169,22 +169,22 @@ QJsonArray GoogleMessage::createToolResultParts(const QHash<QString, QString> &t
|
|||||||
return parts;
|
return parts;
|
||||||
}
|
}
|
||||||
|
|
||||||
QList<LLMCore::ToolUseContent *> GoogleMessage::getCurrentToolUseContent() const
|
QList<PluginLLMCore::ToolUseContent *> GoogleMessage::getCurrentToolUseContent() const
|
||||||
{
|
{
|
||||||
QList<LLMCore::ToolUseContent *> toolBlocks;
|
QList<PluginLLMCore::ToolUseContent *> toolBlocks;
|
||||||
for (auto block : m_currentBlocks) {
|
for (auto block : m_currentBlocks) {
|
||||||
if (auto toolContent = qobject_cast<LLMCore::ToolUseContent *>(block)) {
|
if (auto toolContent = qobject_cast<PluginLLMCore::ToolUseContent *>(block)) {
|
||||||
toolBlocks.append(toolContent);
|
toolBlocks.append(toolContent);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return toolBlocks;
|
return toolBlocks;
|
||||||
}
|
}
|
||||||
|
|
||||||
QList<LLMCore::ThinkingContent *> GoogleMessage::getCurrentThinkingContent() const
|
QList<PluginLLMCore::ThinkingContent *> GoogleMessage::getCurrentThinkingContent() const
|
||||||
{
|
{
|
||||||
QList<LLMCore::ThinkingContent *> thinkingBlocks;
|
QList<PluginLLMCore::ThinkingContent *> thinkingBlocks;
|
||||||
for (auto block : m_currentBlocks) {
|
for (auto block : m_currentBlocks) {
|
||||||
if (auto thinkingContent = qobject_cast<LLMCore::ThinkingContent *>(block)) {
|
if (auto thinkingContent = qobject_cast<PluginLLMCore::ThinkingContent *>(block)) {
|
||||||
thinkingBlocks.append(thinkingContent);
|
thinkingBlocks.append(thinkingContent);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -199,7 +199,7 @@ void GoogleMessage::startNewContinuation()
|
|||||||
m_pendingFunctionArgs.clear();
|
m_pendingFunctionArgs.clear();
|
||||||
m_currentFunctionName.clear();
|
m_currentFunctionName.clear();
|
||||||
m_finishReason.clear();
|
m_finishReason.clear();
|
||||||
m_state = LLMCore::MessageState::Building;
|
m_state = PluginLLMCore::MessageState::Building;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool GoogleMessage::isErrorFinishReason() const
|
bool GoogleMessage::isErrorFinishReason() const
|
||||||
@@ -234,10 +234,10 @@ void GoogleMessage::updateStateFromFinishReason()
|
|||||||
{
|
{
|
||||||
if (m_finishReason == "STOP" || m_finishReason == "MAX_TOKENS") {
|
if (m_finishReason == "STOP" || m_finishReason == "MAX_TOKENS") {
|
||||||
m_state = getCurrentToolUseContent().isEmpty()
|
m_state = getCurrentToolUseContent().isEmpty()
|
||||||
? LLMCore::MessageState::Complete
|
? PluginLLMCore::MessageState::Complete
|
||||||
: LLMCore::MessageState::RequiresToolExecution;
|
: PluginLLMCore::MessageState::RequiresToolExecution;
|
||||||
} else {
|
} else {
|
||||||
m_state = LLMCore::MessageState::Complete;
|
m_state = PluginLLMCore::MessageState::Complete;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -24,7 +24,7 @@
|
|||||||
#include <QJsonObject>
|
#include <QJsonObject>
|
||||||
#include <QObject>
|
#include <QObject>
|
||||||
|
|
||||||
#include <llmcore/ContentBlocks.hpp>
|
#include <pluginllmcore/ContentBlocks.hpp>
|
||||||
|
|
||||||
namespace QodeAssist::Providers {
|
namespace QodeAssist::Providers {
|
||||||
|
|
||||||
@@ -45,11 +45,11 @@ public:
|
|||||||
QJsonObject toProviderFormat() const;
|
QJsonObject toProviderFormat() const;
|
||||||
QJsonArray createToolResultParts(const QHash<QString, QString> &toolResults) const;
|
QJsonArray createToolResultParts(const QHash<QString, QString> &toolResults) const;
|
||||||
|
|
||||||
QList<LLMCore::ToolUseContent *> getCurrentToolUseContent() const;
|
QList<PluginLLMCore::ToolUseContent *> getCurrentToolUseContent() const;
|
||||||
QList<LLMCore::ThinkingContent *> getCurrentThinkingContent() const;
|
QList<PluginLLMCore::ThinkingContent *> getCurrentThinkingContent() const;
|
||||||
QList<LLMCore::ContentBlock *> currentBlocks() const { return m_currentBlocks; }
|
QList<PluginLLMCore::ContentBlock *> currentBlocks() const { return m_currentBlocks; }
|
||||||
|
|
||||||
LLMCore::MessageState state() const { return m_state; }
|
PluginLLMCore::MessageState state() const { return m_state; }
|
||||||
QString finishReason() const { return m_finishReason; }
|
QString finishReason() const { return m_finishReason; }
|
||||||
bool isErrorFinishReason() const;
|
bool isErrorFinishReason() const;
|
||||||
QString getErrorMessage() const;
|
QString getErrorMessage() const;
|
||||||
@@ -58,11 +58,11 @@ public:
|
|||||||
private:
|
private:
|
||||||
void updateStateFromFinishReason();
|
void updateStateFromFinishReason();
|
||||||
|
|
||||||
QList<LLMCore::ContentBlock *> m_currentBlocks;
|
QList<PluginLLMCore::ContentBlock *> m_currentBlocks;
|
||||||
QString m_pendingFunctionArgs;
|
QString m_pendingFunctionArgs;
|
||||||
QString m_currentFunctionName;
|
QString m_currentFunctionName;
|
||||||
QString m_finishReason;
|
QString m_finishReason;
|
||||||
LLMCore::MessageState m_state = LLMCore::MessageState::Building;
|
PluginLLMCore::MessageState m_state = PluginLLMCore::MessageState::Building;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace QodeAssist::Providers
|
} // namespace QodeAssist::Providers
|
||||||
|
|||||||
@@ -19,7 +19,9 @@
|
|||||||
|
|
||||||
#include "LMStudioProvider.hpp"
|
#include "LMStudioProvider.hpp"
|
||||||
|
|
||||||
#include "llmcore/ValidationUtils.hpp"
|
#include <LLMCore/ToolsManager.hpp>
|
||||||
|
|
||||||
|
#include "tools/ToolsRegistration.hpp"
|
||||||
#include "logger/Logger.hpp"
|
#include "logger/Logger.hpp"
|
||||||
#include "settings/ChatAssistantSettings.hpp"
|
#include "settings/ChatAssistantSettings.hpp"
|
||||||
#include "settings/CodeCompletionSettings.hpp"
|
#include "settings/CodeCompletionSettings.hpp"
|
||||||
@@ -34,14 +36,10 @@
|
|||||||
namespace QodeAssist::Providers {
|
namespace QodeAssist::Providers {
|
||||||
|
|
||||||
LMStudioProvider::LMStudioProvider(QObject *parent)
|
LMStudioProvider::LMStudioProvider(QObject *parent)
|
||||||
: LLMCore::Provider(parent)
|
: PluginLLMCore::Provider(parent)
|
||||||
, m_toolsManager(new Tools::ToolsManager(this))
|
, m_client(new ::LLMCore::OpenAIClient(QString(), QString(), QString(), this))
|
||||||
{
|
{
|
||||||
connect(
|
Tools::registerQodeAssistTools(m_client->tools());
|
||||||
m_toolsManager,
|
|
||||||
&Tools::ToolsManager::toolExecutionComplete,
|
|
||||||
this,
|
|
||||||
&LMStudioProvider::onToolExecutionComplete);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
QString LMStudioProvider::name() const
|
QString LMStudioProvider::name() const
|
||||||
@@ -49,6 +47,11 @@ QString LMStudioProvider::name() const
|
|||||||
return "LM Studio";
|
return "LM Studio";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QString LMStudioProvider::apiKey() const
|
||||||
|
{
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
QString LMStudioProvider::url() const
|
QString LMStudioProvider::url() const
|
||||||
{
|
{
|
||||||
return "http://localhost:1234";
|
return "http://localhost:1234";
|
||||||
@@ -64,155 +67,29 @@ QString LMStudioProvider::chatEndpoint() const
|
|||||||
return "/v1/chat/completions";
|
return "/v1/chat/completions";
|
||||||
}
|
}
|
||||||
|
|
||||||
bool LMStudioProvider::supportsModelListing() const
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
QFuture<QList<QString>> LMStudioProvider::getInstalledModels(const QString &url)
|
QFuture<QList<QString>> LMStudioProvider::getInstalledModels(const QString &url)
|
||||||
{
|
{
|
||||||
QNetworkRequest request(QString("%1%2").arg(url, "/v1/models"));
|
m_client->setUrl(url);
|
||||||
|
m_client->setApiKey(apiKey());
|
||||||
return httpClient()->get(request).then([](const QByteArray &data) {
|
return m_client->listModels();
|
||||||
QList<QString> models;
|
|
||||||
QJsonObject jsonObject = QJsonDocument::fromJson(data).object();
|
|
||||||
QJsonArray modelArray = jsonObject["data"].toArray();
|
|
||||||
|
|
||||||
for (const QJsonValue &value : modelArray) {
|
|
||||||
QJsonObject modelObject = value.toObject();
|
|
||||||
models.append(modelObject["id"].toString());
|
|
||||||
}
|
|
||||||
return models;
|
|
||||||
}).onFailed([](const std::exception &e) {
|
|
||||||
LOG_MESSAGE(QString("Error fetching LMStudio models: %1").arg(e.what()));
|
|
||||||
return QList<QString>{};
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
QList<QString> LMStudioProvider::validateRequest(
|
PluginLLMCore::ProviderID LMStudioProvider::providerID() const
|
||||||
const QJsonObject &request, LLMCore::TemplateType type)
|
|
||||||
{
|
{
|
||||||
const auto templateReq = QJsonObject{
|
return PluginLLMCore::ProviderID::LMStudio;
|
||||||
{"model", {}},
|
|
||||||
{"messages", QJsonArray{{QJsonObject{{"role", {}}, {"content", {}}}}}},
|
|
||||||
{"temperature", {}},
|
|
||||||
{"max_tokens", {}},
|
|
||||||
{"top_p", {}},
|
|
||||||
{"top_k", {}},
|
|
||||||
{"frequency_penalty", {}},
|
|
||||||
{"presence_penalty", {}},
|
|
||||||
{"stop", QJsonArray{}},
|
|
||||||
{"stream", {}},
|
|
||||||
{"tools", {}}};
|
|
||||||
|
|
||||||
return LLMCore::ValidationUtils::validateRequestFields(request, templateReq);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
QString LMStudioProvider::apiKey() const
|
PluginLLMCore::ProviderCapabilities LMStudioProvider::capabilities() const
|
||||||
{
|
{
|
||||||
return {};
|
return PluginLLMCore::ProviderCapability::Tools | PluginLLMCore::ProviderCapability::Image
|
||||||
}
|
| PluginLLMCore::ProviderCapability::ModelListing;
|
||||||
|
|
||||||
void LMStudioProvider::prepareNetworkRequest(QNetworkRequest &networkRequest) const
|
|
||||||
{
|
|
||||||
networkRequest.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
|
|
||||||
}
|
|
||||||
|
|
||||||
LLMCore::ProviderID LMStudioProvider::providerID() const
|
|
||||||
{
|
|
||||||
return LLMCore::ProviderID::LMStudio;
|
|
||||||
}
|
|
||||||
|
|
||||||
void LMStudioProvider::sendRequest(
|
|
||||||
const LLMCore::RequestID &requestId, const QUrl &url, const QJsonObject &payload)
|
|
||||||
{
|
|
||||||
if (!m_messages.contains(requestId)) {
|
|
||||||
m_dataBuffers[requestId].clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
m_requestUrls[requestId] = url;
|
|
||||||
m_originalRequests[requestId] = payload;
|
|
||||||
|
|
||||||
QNetworkRequest networkRequest(url);
|
|
||||||
prepareNetworkRequest(networkRequest);
|
|
||||||
|
|
||||||
LOG_MESSAGE(
|
|
||||||
QString("LMStudioProvider: Sending request %1 to %2").arg(requestId, url.toString()));
|
|
||||||
|
|
||||||
httpClient()->postStreaming(requestId, networkRequest, payload);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool LMStudioProvider::supportsTools() const
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool LMStudioProvider::supportImage() const
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
void LMStudioProvider::cancelRequest(const LLMCore::RequestID &requestId)
|
|
||||||
{
|
|
||||||
LOG_MESSAGE(QString("LMStudioProvider: Cancelling request %1").arg(requestId));
|
|
||||||
LLMCore::Provider::cancelRequest(requestId);
|
|
||||||
cleanupRequest(requestId);
|
|
||||||
}
|
|
||||||
|
|
||||||
void LMStudioProvider::onDataReceived(
|
|
||||||
const QodeAssist::LLMCore::RequestID &requestId, const QByteArray &data)
|
|
||||||
{
|
|
||||||
LLMCore::DataBuffers &buffers = m_dataBuffers[requestId];
|
|
||||||
QStringList lines = buffers.rawStreamBuffer.processData(data);
|
|
||||||
|
|
||||||
for (const QString &line : lines) {
|
|
||||||
if (line.trimmed().isEmpty() || line == "data: [DONE]") {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
QJsonObject chunk = parseEventLine(line);
|
|
||||||
if (chunk.isEmpty())
|
|
||||||
continue;
|
|
||||||
|
|
||||||
processStreamChunk(requestId, chunk);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void LMStudioProvider::onRequestFinished(
|
|
||||||
const QodeAssist::LLMCore::RequestID &requestId, std::optional<QString> error)
|
|
||||||
{
|
|
||||||
if (error) {
|
|
||||||
LOG_MESSAGE(QString("LMStudioProvider request %1 failed: %2").arg(requestId, *error));
|
|
||||||
emit requestFailed(requestId, *error);
|
|
||||||
cleanupRequest(requestId);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (m_messages.contains(requestId)) {
|
|
||||||
OpenAIMessage *message = m_messages[requestId];
|
|
||||||
if (message->state() == LLMCore::MessageState::RequiresToolExecution) {
|
|
||||||
LOG_MESSAGE(QString("Waiting for tools to complete for %1").arg(requestId));
|
|
||||||
m_dataBuffers.remove(requestId);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (m_dataBuffers.contains(requestId)) {
|
|
||||||
const LLMCore::DataBuffers &buffers = m_dataBuffers[requestId];
|
|
||||||
if (!buffers.responseContent.isEmpty()) {
|
|
||||||
LOG_MESSAGE(QString("Emitting full response for %1").arg(requestId));
|
|
||||||
emit fullResponseReceived(requestId, buffers.responseContent);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
cleanupRequest(requestId);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void LMStudioProvider::prepareRequest(
|
void LMStudioProvider::prepareRequest(
|
||||||
QJsonObject &request,
|
QJsonObject &request,
|
||||||
LLMCore::PromptTemplate *prompt,
|
PluginLLMCore::PromptTemplate *prompt,
|
||||||
LLMCore::ContextData context,
|
PluginLLMCore::ContextData context,
|
||||||
LLMCore::RequestType type,
|
PluginLLMCore::RequestType type,
|
||||||
bool isToolsEnabled,
|
bool isToolsEnabled,
|
||||||
bool isThinkingEnabled)
|
bool isThinkingEnabled)
|
||||||
{
|
{
|
||||||
@@ -236,22 +113,16 @@ void LMStudioProvider::prepareRequest(
|
|||||||
request["presence_penalty"] = settings.presencePenalty();
|
request["presence_penalty"] = settings.presencePenalty();
|
||||||
};
|
};
|
||||||
|
|
||||||
if (type == LLMCore::RequestType::CodeCompletion) {
|
if (type == PluginLLMCore::RequestType::CodeCompletion) {
|
||||||
applyModelParams(Settings::codeCompletionSettings());
|
applyModelParams(Settings::codeCompletionSettings());
|
||||||
} else if (type == LLMCore::RequestType::QuickRefactoring) {
|
} else if (type == PluginLLMCore::RequestType::QuickRefactoring) {
|
||||||
applyModelParams(Settings::quickRefactorSettings());
|
applyModelParams(Settings::quickRefactorSettings());
|
||||||
} else {
|
} else {
|
||||||
applyModelParams(Settings::chatAssistantSettings());
|
applyModelParams(Settings::chatAssistantSettings());
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isToolsEnabled) {
|
if (isToolsEnabled) {
|
||||||
LLMCore::RunToolsFilter filter = LLMCore::RunToolsFilter::ALL;
|
auto toolsDefinitions = m_client->tools()->getToolsDefinitions();
|
||||||
if (type == LLMCore::RequestType::QuickRefactoring) {
|
|
||||||
filter = LLMCore::RunToolsFilter::OnlyRead;
|
|
||||||
}
|
|
||||||
|
|
||||||
auto toolsDefinitions = m_toolsManager->getToolsDefinitions(
|
|
||||||
LLMCore::ToolSchemaFormat::OpenAI, filter);
|
|
||||||
if (!toolsDefinitions.isEmpty()) {
|
if (!toolsDefinitions.isEmpty()) {
|
||||||
request["tools"] = toolsDefinitions;
|
request["tools"] = toolsDefinitions;
|
||||||
LOG_MESSAGE(QString("Added %1 tools to LMStudio request").arg(toolsDefinitions.size()));
|
LOG_MESSAGE(QString("Added %1 tools to LMStudio request").arg(toolsDefinitions.size()));
|
||||||
@@ -259,163 +130,9 @@ void LMStudioProvider::prepareRequest(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void LMStudioProvider::onToolExecutionComplete(
|
::LLMCore::BaseClient *LMStudioProvider::client() const
|
||||||
const QString &requestId, const QHash<QString, QString> &toolResults)
|
|
||||||
{
|
{
|
||||||
if (!m_messages.contains(requestId) || !m_requestUrls.contains(requestId)) {
|
return m_client;
|
||||||
LOG_MESSAGE(QString("ERROR: Missing data for continuation request %1").arg(requestId));
|
|
||||||
cleanupRequest(requestId);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
LOG_MESSAGE(QString("Tool execution complete for LMStudio request %1").arg(requestId));
|
|
||||||
|
|
||||||
for (auto it = toolResults.begin(); it != toolResults.end(); ++it) {
|
|
||||||
OpenAIMessage *message = m_messages[requestId];
|
|
||||||
auto toolContent = message->getCurrentToolUseContent();
|
|
||||||
for (auto tool : toolContent) {
|
|
||||||
if (tool->id() == it.key()) {
|
|
||||||
auto toolStringName = m_toolsManager->toolsFactory()->getStringName(tool->name());
|
|
||||||
emit toolExecutionCompleted(
|
|
||||||
requestId, tool->id(), toolStringName, toolResults[tool->id()]);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
OpenAIMessage *message = m_messages[requestId];
|
|
||||||
QJsonObject continuationRequest = m_originalRequests[requestId];
|
|
||||||
QJsonArray messages = continuationRequest["messages"].toArray();
|
|
||||||
|
|
||||||
messages.append(message->toProviderFormat());
|
|
||||||
|
|
||||||
QJsonArray toolResultMessages = message->createToolResultMessages(toolResults);
|
|
||||||
for (const auto &toolMsg : toolResultMessages) {
|
|
||||||
messages.append(toolMsg);
|
|
||||||
}
|
|
||||||
|
|
||||||
continuationRequest["messages"] = messages;
|
|
||||||
|
|
||||||
LOG_MESSAGE(QString("Sending continuation request for %1 with %2 tool results")
|
|
||||||
.arg(requestId)
|
|
||||||
.arg(toolResults.size()));
|
|
||||||
|
|
||||||
sendRequest(requestId, m_requestUrls[requestId], continuationRequest);
|
|
||||||
}
|
|
||||||
|
|
||||||
void LMStudioProvider::processStreamChunk(const QString &requestId, const QJsonObject &chunk)
|
|
||||||
{
|
|
||||||
QJsonArray choices = chunk["choices"].toArray();
|
|
||||||
if (choices.isEmpty()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
QJsonObject choice = choices[0].toObject();
|
|
||||||
QJsonObject delta = choice["delta"].toObject();
|
|
||||||
QString finishReason = choice["finish_reason"].toString();
|
|
||||||
|
|
||||||
OpenAIMessage *message = m_messages.value(requestId);
|
|
||||||
if (!message) {
|
|
||||||
message = new OpenAIMessage(this);
|
|
||||||
m_messages[requestId] = message;
|
|
||||||
LOG_MESSAGE(QString("Created NEW OpenAIMessage for request %1").arg(requestId));
|
|
||||||
|
|
||||||
if (m_dataBuffers.contains(requestId)) {
|
|
||||||
emit continuationStarted(requestId);
|
|
||||||
LOG_MESSAGE(QString("Starting continuation for request %1").arg(requestId));
|
|
||||||
}
|
|
||||||
} else if (
|
|
||||||
m_dataBuffers.contains(requestId)
|
|
||||||
&& message->state() == LLMCore::MessageState::RequiresToolExecution) {
|
|
||||||
message->startNewContinuation();
|
|
||||||
emit continuationStarted(requestId);
|
|
||||||
LOG_MESSAGE(QString("Cleared message state for continuation request %1").arg(requestId));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (delta.contains("content") && !delta["content"].isNull()) {
|
|
||||||
QString content = delta["content"].toString();
|
|
||||||
message->handleContentDelta(content);
|
|
||||||
|
|
||||||
LLMCore::DataBuffers &buffers = m_dataBuffers[requestId];
|
|
||||||
buffers.responseContent += content;
|
|
||||||
emit partialResponseReceived(requestId, content);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (delta.contains("tool_calls")) {
|
|
||||||
QJsonArray toolCalls = delta["tool_calls"].toArray();
|
|
||||||
for (const auto &toolCallValue : toolCalls) {
|
|
||||||
QJsonObject toolCall = toolCallValue.toObject();
|
|
||||||
int index = toolCall["index"].toInt();
|
|
||||||
|
|
||||||
if (toolCall.contains("id")) {
|
|
||||||
QString id = toolCall["id"].toString();
|
|
||||||
QJsonObject function = toolCall["function"].toObject();
|
|
||||||
QString name = function["name"].toString();
|
|
||||||
message->handleToolCallStart(index, id, name);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (toolCall.contains("function")) {
|
|
||||||
QJsonObject function = toolCall["function"].toObject();
|
|
||||||
if (function.contains("arguments")) {
|
|
||||||
QString args = function["arguments"].toString();
|
|
||||||
message->handleToolCallDelta(index, args);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!finishReason.isEmpty() && finishReason != "null") {
|
|
||||||
for (int i = 0; i < 10; ++i) {
|
|
||||||
message->handleToolCallComplete(i);
|
|
||||||
}
|
|
||||||
|
|
||||||
message->handleFinishReason(finishReason);
|
|
||||||
handleMessageComplete(requestId);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void LMStudioProvider::handleMessageComplete(const QString &requestId)
|
|
||||||
{
|
|
||||||
if (!m_messages.contains(requestId))
|
|
||||||
return;
|
|
||||||
|
|
||||||
OpenAIMessage *message = m_messages[requestId];
|
|
||||||
|
|
||||||
if (message->state() == LLMCore::MessageState::RequiresToolExecution) {
|
|
||||||
LOG_MESSAGE(QString("LMStudio message requires tool execution for %1").arg(requestId));
|
|
||||||
|
|
||||||
auto toolUseContent = message->getCurrentToolUseContent();
|
|
||||||
|
|
||||||
if (toolUseContent.isEmpty()) {
|
|
||||||
LOG_MESSAGE(QString("No tools to execute for %1").arg(requestId));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (auto toolContent : toolUseContent) {
|
|
||||||
auto toolStringName = m_toolsManager->toolsFactory()->getStringName(toolContent->name());
|
|
||||||
emit toolExecutionStarted(requestId, toolContent->id(), toolStringName);
|
|
||||||
m_toolsManager->executeToolCall(
|
|
||||||
requestId, toolContent->id(), toolContent->name(), toolContent->input());
|
|
||||||
}
|
|
||||||
|
|
||||||
} else {
|
|
||||||
LOG_MESSAGE(QString("LMStudio message marked as complete for %1").arg(requestId));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void LMStudioProvider::cleanupRequest(const LLMCore::RequestID &requestId)
|
|
||||||
{
|
|
||||||
LOG_MESSAGE(QString("Cleaning up LMStudio request %1").arg(requestId));
|
|
||||||
|
|
||||||
if (m_messages.contains(requestId)) {
|
|
||||||
OpenAIMessage *message = m_messages.take(requestId);
|
|
||||||
message->deleteLater();
|
|
||||||
}
|
|
||||||
|
|
||||||
m_dataBuffers.remove(requestId);
|
|
||||||
m_requestUrls.remove(requestId);
|
|
||||||
m_originalRequests.remove(requestId);
|
|
||||||
m_toolsManager->cleanupRequest(requestId);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace QodeAssist::Providers
|
} // namespace QodeAssist::Providers
|
||||||
|
|||||||
@@ -19,13 +19,12 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "OpenAIMessage.hpp"
|
#include <LLMCore/OpenAIClient.hpp>
|
||||||
#include "tools/ToolsManager.hpp"
|
#include <pluginllmcore/Provider.hpp>
|
||||||
#include <llmcore/Provider.hpp>
|
|
||||||
|
|
||||||
namespace QodeAssist::Providers {
|
namespace QodeAssist::Providers {
|
||||||
|
|
||||||
class LMStudioProvider : public LLMCore::Provider
|
class LMStudioProvider : public PluginLLMCore::Provider
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
public:
|
public:
|
||||||
@@ -35,47 +34,22 @@ public:
|
|||||||
QString url() const override;
|
QString url() const override;
|
||||||
QString completionEndpoint() const override;
|
QString completionEndpoint() const override;
|
||||||
QString chatEndpoint() const override;
|
QString chatEndpoint() const override;
|
||||||
bool supportsModelListing() const override;
|
|
||||||
void prepareRequest(
|
void prepareRequest(
|
||||||
QJsonObject &request,
|
QJsonObject &request,
|
||||||
LLMCore::PromptTemplate *prompt,
|
PluginLLMCore::PromptTemplate *prompt,
|
||||||
LLMCore::ContextData context,
|
PluginLLMCore::ContextData context,
|
||||||
LLMCore::RequestType type,
|
PluginLLMCore::RequestType type,
|
||||||
bool isToolsEnabled,
|
bool isToolsEnabled,
|
||||||
bool isThinkingEnabled) override;
|
bool isThinkingEnabled) override;
|
||||||
QFuture<QList<QString>> getInstalledModels(const QString &url) override;
|
QFuture<QList<QString>> getInstalledModels(const QString &url) override;
|
||||||
QList<QString> validateRequest(const QJsonObject &request, LLMCore::TemplateType type) override;
|
PluginLLMCore::ProviderID providerID() const override;
|
||||||
|
PluginLLMCore::ProviderCapabilities capabilities() const override;
|
||||||
|
|
||||||
|
::LLMCore::BaseClient *client() const override;
|
||||||
QString apiKey() const override;
|
QString apiKey() const override;
|
||||||
void prepareNetworkRequest(QNetworkRequest &networkRequest) const override;
|
|
||||||
LLMCore::ProviderID providerID() const override;
|
|
||||||
|
|
||||||
void sendRequest(
|
|
||||||
const LLMCore::RequestID &requestId, const QUrl &url, const QJsonObject &payload) override;
|
|
||||||
|
|
||||||
bool supportsTools() const override;
|
|
||||||
bool supportImage() const override;
|
|
||||||
void cancelRequest(const LLMCore::RequestID &requestId) override;
|
|
||||||
|
|
||||||
public slots:
|
|
||||||
void onDataReceived(
|
|
||||||
const QodeAssist::LLMCore::RequestID &requestId, const QByteArray &data) override;
|
|
||||||
void onRequestFinished(
|
|
||||||
const QodeAssist::LLMCore::RequestID &requestId,
|
|
||||||
std::optional<QString> error) override;
|
|
||||||
|
|
||||||
private slots:
|
|
||||||
void onToolExecutionComplete(
|
|
||||||
const QString &requestId, const QHash<QString, QString> &toolResults);
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void processStreamChunk(const QString &requestId, const QJsonObject &chunk);
|
::LLMCore::OpenAIClient *m_client;
|
||||||
void handleMessageComplete(const QString &requestId);
|
|
||||||
void cleanupRequest(const LLMCore::RequestID &requestId);
|
|
||||||
|
|
||||||
QHash<LLMCore::RequestID, OpenAIMessage *> m_messages;
|
|
||||||
QHash<LLMCore::RequestID, QUrl> m_requestUrls;
|
|
||||||
QHash<LLMCore::RequestID, QJsonObject> m_originalRequests;
|
|
||||||
Tools::ToolsManager *m_toolsManager;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace QodeAssist::Providers
|
} // namespace QodeAssist::Providers
|
||||||
|
|||||||
@@ -19,12 +19,13 @@
|
|||||||
|
|
||||||
#include "LlamaCppProvider.hpp"
|
#include "LlamaCppProvider.hpp"
|
||||||
|
|
||||||
#include "llmcore/ValidationUtils.hpp"
|
#include <LLMCore/ToolsManager.hpp>
|
||||||
#include "logger/Logger.hpp"
|
#include "logger/Logger.hpp"
|
||||||
#include "settings/ChatAssistantSettings.hpp"
|
#include "settings/ChatAssistantSettings.hpp"
|
||||||
#include "settings/CodeCompletionSettings.hpp"
|
#include "settings/CodeCompletionSettings.hpp"
|
||||||
#include "settings/QuickRefactorSettings.hpp"
|
#include "settings/QuickRefactorSettings.hpp"
|
||||||
#include "settings/GeneralSettings.hpp"
|
#include "settings/GeneralSettings.hpp"
|
||||||
|
#include "tools/ToolsRegistration.hpp"
|
||||||
|
|
||||||
#include <QJsonArray>
|
#include <QJsonArray>
|
||||||
#include <QJsonDocument>
|
#include <QJsonDocument>
|
||||||
@@ -33,14 +34,10 @@
|
|||||||
namespace QodeAssist::Providers {
|
namespace QodeAssist::Providers {
|
||||||
|
|
||||||
LlamaCppProvider::LlamaCppProvider(QObject *parent)
|
LlamaCppProvider::LlamaCppProvider(QObject *parent)
|
||||||
: LLMCore::Provider(parent)
|
: PluginLLMCore::Provider(parent)
|
||||||
, m_toolsManager(new Tools::ToolsManager(this))
|
, m_client(new ::LLMCore::LlamaCppClient(QString(), QString(), QString(), this))
|
||||||
{
|
{
|
||||||
connect(
|
Tools::registerQodeAssistTools(m_client->tools());
|
||||||
m_toolsManager,
|
|
||||||
&Tools::ToolsManager::toolExecutionComplete,
|
|
||||||
this,
|
|
||||||
&LlamaCppProvider::onToolExecutionComplete);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
QString LlamaCppProvider::name() const
|
QString LlamaCppProvider::name() const
|
||||||
@@ -48,6 +45,11 @@ QString LlamaCppProvider::name() const
|
|||||||
return "llama.cpp";
|
return "llama.cpp";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QString LlamaCppProvider::apiKey() const
|
||||||
|
{
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
QString LlamaCppProvider::url() const
|
QString LlamaCppProvider::url() const
|
||||||
{
|
{
|
||||||
return "http://localhost:8080";
|
return "http://localhost:8080";
|
||||||
@@ -63,16 +65,11 @@ QString LlamaCppProvider::chatEndpoint() const
|
|||||||
return "/v1/chat/completions";
|
return "/v1/chat/completions";
|
||||||
}
|
}
|
||||||
|
|
||||||
bool LlamaCppProvider::supportsModelListing() const
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
void LlamaCppProvider::prepareRequest(
|
void LlamaCppProvider::prepareRequest(
|
||||||
QJsonObject &request,
|
QJsonObject &request,
|
||||||
LLMCore::PromptTemplate *prompt,
|
PluginLLMCore::PromptTemplate *prompt,
|
||||||
LLMCore::ContextData context,
|
PluginLLMCore::ContextData context,
|
||||||
LLMCore::RequestType type,
|
PluginLLMCore::RequestType type,
|
||||||
bool isToolsEnabled,
|
bool isToolsEnabled,
|
||||||
bool isThinkingEnabled)
|
bool isThinkingEnabled)
|
||||||
{
|
{
|
||||||
@@ -96,22 +93,16 @@ void LlamaCppProvider::prepareRequest(
|
|||||||
request["presence_penalty"] = settings.presencePenalty();
|
request["presence_penalty"] = settings.presencePenalty();
|
||||||
};
|
};
|
||||||
|
|
||||||
if (type == LLMCore::RequestType::CodeCompletion) {
|
if (type == PluginLLMCore::RequestType::CodeCompletion) {
|
||||||
applyModelParams(Settings::codeCompletionSettings());
|
applyModelParams(Settings::codeCompletionSettings());
|
||||||
} else if (type == LLMCore::RequestType::QuickRefactoring) {
|
} else if (type == PluginLLMCore::RequestType::QuickRefactoring) {
|
||||||
applyModelParams(Settings::quickRefactorSettings());
|
applyModelParams(Settings::quickRefactorSettings());
|
||||||
} else {
|
} else {
|
||||||
applyModelParams(Settings::chatAssistantSettings());
|
applyModelParams(Settings::chatAssistantSettings());
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isToolsEnabled) {
|
if (isToolsEnabled) {
|
||||||
LLMCore::RunToolsFilter filter = LLMCore::RunToolsFilter::ALL;
|
auto toolsDefinitions = m_client->tools()->getToolsDefinitions();
|
||||||
if (type == LLMCore::RequestType::QuickRefactoring) {
|
|
||||||
filter = LLMCore::RunToolsFilter::OnlyRead;
|
|
||||||
}
|
|
||||||
|
|
||||||
auto toolsDefinitions = m_toolsManager->getToolsDefinitions(
|
|
||||||
LLMCore::ToolSchemaFormat::OpenAI, filter);
|
|
||||||
if (!toolsDefinitions.isEmpty()) {
|
if (!toolsDefinitions.isEmpty()) {
|
||||||
request["tools"] = toolsDefinitions;
|
request["tools"] = toolsDefinitions;
|
||||||
LOG_MESSAGE(QString("Added %1 tools to llama.cpp request").arg(toolsDefinitions.size()));
|
LOG_MESSAGE(QString("Added %1 tools to llama.cpp request").arg(toolsDefinitions.size()));
|
||||||
@@ -124,313 +115,19 @@ QFuture<QList<QString>> LlamaCppProvider::getInstalledModels(const QString &)
|
|||||||
return QtFuture::makeReadyFuture(QList<QString>{});
|
return QtFuture::makeReadyFuture(QList<QString>{});
|
||||||
}
|
}
|
||||||
|
|
||||||
QList<QString> LlamaCppProvider::validateRequest(
|
PluginLLMCore::ProviderID LlamaCppProvider::providerID() const
|
||||||
const QJsonObject &request, LLMCore::TemplateType type)
|
|
||||||
{
|
{
|
||||||
if (type == LLMCore::TemplateType::FIM) {
|
return PluginLLMCore::ProviderID::LlamaCpp;
|
||||||
const auto infillReq = QJsonObject{
|
|
||||||
{"model", {}},
|
|
||||||
{"input_prefix", {}},
|
|
||||||
{"input_suffix", {}},
|
|
||||||
{"input_extra", {}},
|
|
||||||
{"prompt", {}},
|
|
||||||
{"temperature", {}},
|
|
||||||
{"top_p", {}},
|
|
||||||
{"top_k", {}},
|
|
||||||
{"max_tokens", {}},
|
|
||||||
{"frequency_penalty", {}},
|
|
||||||
{"presence_penalty", {}},
|
|
||||||
{"stop", QJsonArray{}},
|
|
||||||
{"stream", {}}};
|
|
||||||
|
|
||||||
return LLMCore::ValidationUtils::validateRequestFields(request, infillReq);
|
|
||||||
} else {
|
|
||||||
const auto chatReq = QJsonObject{
|
|
||||||
{"model", {}},
|
|
||||||
{"messages", QJsonArray{{QJsonObject{{"role", {}}, {"content", {}}}}}},
|
|
||||||
{"temperature", {}},
|
|
||||||
{"max_tokens", {}},
|
|
||||||
{"top_p", {}},
|
|
||||||
{"top_k", {}},
|
|
||||||
{"frequency_penalty", {}},
|
|
||||||
{"presence_penalty", {}},
|
|
||||||
{"stop", QJsonArray{}},
|
|
||||||
{"stream", {}},
|
|
||||||
{"tools", {}}};
|
|
||||||
|
|
||||||
return LLMCore::ValidationUtils::validateRequestFields(request, chatReq);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
QString LlamaCppProvider::apiKey() const
|
PluginLLMCore::ProviderCapabilities LlamaCppProvider::capabilities() const
|
||||||
{
|
{
|
||||||
return {};
|
return PluginLLMCore::ProviderCapability::Tools | PluginLLMCore::ProviderCapability::Image;
|
||||||
}
|
}
|
||||||
|
|
||||||
void LlamaCppProvider::prepareNetworkRequest(QNetworkRequest &networkRequest) const
|
::LLMCore::BaseClient *LlamaCppProvider::client() const
|
||||||
{
|
{
|
||||||
networkRequest.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
|
return m_client;
|
||||||
}
|
|
||||||
|
|
||||||
LLMCore::ProviderID LlamaCppProvider::providerID() const
|
|
||||||
{
|
|
||||||
return LLMCore::ProviderID::LlamaCpp;
|
|
||||||
}
|
|
||||||
|
|
||||||
void LlamaCppProvider::sendRequest(
|
|
||||||
const LLMCore::RequestID &requestId, const QUrl &url, const QJsonObject &payload)
|
|
||||||
{
|
|
||||||
if (!m_messages.contains(requestId)) {
|
|
||||||
m_dataBuffers[requestId].clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
m_requestUrls[requestId] = url;
|
|
||||||
m_originalRequests[requestId] = payload;
|
|
||||||
|
|
||||||
QNetworkRequest networkRequest(url);
|
|
||||||
prepareNetworkRequest(networkRequest);
|
|
||||||
|
|
||||||
LOG_MESSAGE(
|
|
||||||
QString("LlamaCppProvider: Sending request %1 to %2").arg(requestId, url.toString()));
|
|
||||||
|
|
||||||
httpClient()->postStreaming(requestId, networkRequest, payload);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool LlamaCppProvider::supportsTools() const
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool LlamaCppProvider::supportImage() const
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
void LlamaCppProvider::cancelRequest(const LLMCore::RequestID &requestId)
|
|
||||||
{
|
|
||||||
LOG_MESSAGE(QString("LlamaCppProvider: Cancelling request %1").arg(requestId));
|
|
||||||
LLMCore::Provider::cancelRequest(requestId);
|
|
||||||
cleanupRequest(requestId);
|
|
||||||
}
|
|
||||||
|
|
||||||
void LlamaCppProvider::onDataReceived(
|
|
||||||
const QodeAssist::LLMCore::RequestID &requestId, const QByteArray &data)
|
|
||||||
{
|
|
||||||
LLMCore::DataBuffers &buffers = m_dataBuffers[requestId];
|
|
||||||
QStringList lines = buffers.rawStreamBuffer.processData(data);
|
|
||||||
|
|
||||||
for (const QString &line : lines) {
|
|
||||||
if (line.trimmed().isEmpty() || line == "data: [DONE]") {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
QJsonObject chunk = parseEventLine(line);
|
|
||||||
if (chunk.isEmpty())
|
|
||||||
continue;
|
|
||||||
|
|
||||||
if (chunk.contains("content")) {
|
|
||||||
QString content = chunk["content"].toString();
|
|
||||||
if (!content.isEmpty()) {
|
|
||||||
buffers.responseContent += content;
|
|
||||||
emit partialResponseReceived(requestId, content);
|
|
||||||
}
|
|
||||||
if (chunk["stop"].toBool()) {
|
|
||||||
emit fullResponseReceived(requestId, buffers.responseContent);
|
|
||||||
m_dataBuffers.remove(requestId);
|
|
||||||
}
|
|
||||||
} else if (chunk.contains("choices")) {
|
|
||||||
processStreamChunk(requestId, chunk);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void LlamaCppProvider::onRequestFinished(
|
|
||||||
const QodeAssist::LLMCore::RequestID &requestId, std::optional<QString> error)
|
|
||||||
{
|
|
||||||
if (error) {
|
|
||||||
LOG_MESSAGE(QString("LlamaCppProvider request %1 failed: %2").arg(requestId, *error));
|
|
||||||
emit requestFailed(requestId, *error);
|
|
||||||
cleanupRequest(requestId);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (m_messages.contains(requestId)) {
|
|
||||||
OpenAIMessage *message = m_messages[requestId];
|
|
||||||
if (message->state() == LLMCore::MessageState::RequiresToolExecution) {
|
|
||||||
LOG_MESSAGE(QString("Waiting for tools to complete for %1").arg(requestId));
|
|
||||||
m_dataBuffers.remove(requestId);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (m_dataBuffers.contains(requestId)) {
|
|
||||||
const LLMCore::DataBuffers &buffers = m_dataBuffers[requestId];
|
|
||||||
if (!buffers.responseContent.isEmpty()) {
|
|
||||||
LOG_MESSAGE(QString("Emitting full response for %1").arg(requestId));
|
|
||||||
emit fullResponseReceived(requestId, buffers.responseContent);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
cleanupRequest(requestId);
|
|
||||||
}
|
|
||||||
|
|
||||||
void LlamaCppProvider::onToolExecutionComplete(
|
|
||||||
const QString &requestId, const QHash<QString, QString> &toolResults)
|
|
||||||
{
|
|
||||||
if (!m_messages.contains(requestId) || !m_requestUrls.contains(requestId)) {
|
|
||||||
LOG_MESSAGE(QString("ERROR: Missing data for continuation request %1").arg(requestId));
|
|
||||||
cleanupRequest(requestId);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
LOG_MESSAGE(QString("Tool execution complete for llama.cpp request %1").arg(requestId));
|
|
||||||
|
|
||||||
for (auto it = toolResults.begin(); it != toolResults.end(); ++it) {
|
|
||||||
OpenAIMessage *message = m_messages[requestId];
|
|
||||||
auto toolContent = message->getCurrentToolUseContent();
|
|
||||||
for (auto tool : toolContent) {
|
|
||||||
if (tool->id() == it.key()) {
|
|
||||||
auto toolStringName = m_toolsManager->toolsFactory()->getStringName(tool->name());
|
|
||||||
emit toolExecutionCompleted(
|
|
||||||
requestId, tool->id(), toolStringName, toolResults[tool->id()]);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
OpenAIMessage *message = m_messages[requestId];
|
|
||||||
QJsonObject continuationRequest = m_originalRequests[requestId];
|
|
||||||
QJsonArray messages = continuationRequest["messages"].toArray();
|
|
||||||
|
|
||||||
messages.append(message->toProviderFormat());
|
|
||||||
|
|
||||||
QJsonArray toolResultMessages = message->createToolResultMessages(toolResults);
|
|
||||||
for (const auto &toolMsg : toolResultMessages) {
|
|
||||||
messages.append(toolMsg);
|
|
||||||
}
|
|
||||||
|
|
||||||
continuationRequest["messages"] = messages;
|
|
||||||
|
|
||||||
LOG_MESSAGE(QString("Sending continuation request for %1 with %2 tool results")
|
|
||||||
.arg(requestId)
|
|
||||||
.arg(toolResults.size()));
|
|
||||||
|
|
||||||
sendRequest(requestId, m_requestUrls[requestId], continuationRequest);
|
|
||||||
}
|
|
||||||
|
|
||||||
void LlamaCppProvider::processStreamChunk(const QString &requestId, const QJsonObject &chunk)
|
|
||||||
{
|
|
||||||
QJsonArray choices = chunk["choices"].toArray();
|
|
||||||
if (choices.isEmpty()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
QJsonObject choice = choices[0].toObject();
|
|
||||||
QJsonObject delta = choice["delta"].toObject();
|
|
||||||
QString finishReason = choice["finish_reason"].toString();
|
|
||||||
|
|
||||||
OpenAIMessage *message = m_messages.value(requestId);
|
|
||||||
if (!message) {
|
|
||||||
message = new OpenAIMessage(this);
|
|
||||||
m_messages[requestId] = message;
|
|
||||||
LOG_MESSAGE(QString("Created NEW OpenAIMessage for llama.cpp request %1").arg(requestId));
|
|
||||||
|
|
||||||
if (m_dataBuffers.contains(requestId)) {
|
|
||||||
emit continuationStarted(requestId);
|
|
||||||
LOG_MESSAGE(QString("Starting continuation for request %1").arg(requestId));
|
|
||||||
}
|
|
||||||
} else if (
|
|
||||||
m_dataBuffers.contains(requestId)
|
|
||||||
&& message->state() == LLMCore::MessageState::RequiresToolExecution) {
|
|
||||||
message->startNewContinuation();
|
|
||||||
emit continuationStarted(requestId);
|
|
||||||
LOG_MESSAGE(QString("Cleared message state for continuation request %1").arg(requestId));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (delta.contains("content") && !delta["content"].isNull()) {
|
|
||||||
QString content = delta["content"].toString();
|
|
||||||
message->handleContentDelta(content);
|
|
||||||
|
|
||||||
LLMCore::DataBuffers &buffers = m_dataBuffers[requestId];
|
|
||||||
buffers.responseContent += content;
|
|
||||||
emit partialResponseReceived(requestId, content);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (delta.contains("tool_calls")) {
|
|
||||||
QJsonArray toolCalls = delta["tool_calls"].toArray();
|
|
||||||
for (const auto &toolCallValue : toolCalls) {
|
|
||||||
QJsonObject toolCall = toolCallValue.toObject();
|
|
||||||
int index = toolCall["index"].toInt();
|
|
||||||
|
|
||||||
if (toolCall.contains("id")) {
|
|
||||||
QString id = toolCall["id"].toString();
|
|
||||||
QJsonObject function = toolCall["function"].toObject();
|
|
||||||
QString name = function["name"].toString();
|
|
||||||
message->handleToolCallStart(index, id, name);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (toolCall.contains("function")) {
|
|
||||||
QJsonObject function = toolCall["function"].toObject();
|
|
||||||
if (function.contains("arguments")) {
|
|
||||||
QString args = function["arguments"].toString();
|
|
||||||
message->handleToolCallDelta(index, args);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!finishReason.isEmpty() && finishReason != "null") {
|
|
||||||
for (int i = 0; i < 10; ++i) {
|
|
||||||
message->handleToolCallComplete(i);
|
|
||||||
}
|
|
||||||
|
|
||||||
message->handleFinishReason(finishReason);
|
|
||||||
handleMessageComplete(requestId);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void LlamaCppProvider::handleMessageComplete(const QString &requestId)
|
|
||||||
{
|
|
||||||
if (!m_messages.contains(requestId))
|
|
||||||
return;
|
|
||||||
|
|
||||||
OpenAIMessage *message = m_messages[requestId];
|
|
||||||
|
|
||||||
if (message->state() == LLMCore::MessageState::RequiresToolExecution) {
|
|
||||||
LOG_MESSAGE(QString("llama.cpp message requires tool execution for %1").arg(requestId));
|
|
||||||
|
|
||||||
auto toolUseContent = message->getCurrentToolUseContent();
|
|
||||||
|
|
||||||
if (toolUseContent.isEmpty()) {
|
|
||||||
LOG_MESSAGE(QString("No tools to execute for %1").arg(requestId));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (auto toolContent : toolUseContent) {
|
|
||||||
auto toolStringName = m_toolsManager->toolsFactory()->getStringName(toolContent->name());
|
|
||||||
emit toolExecutionStarted(requestId, toolContent->id(), toolStringName);
|
|
||||||
m_toolsManager->executeToolCall(
|
|
||||||
requestId, toolContent->id(), toolContent->name(), toolContent->input());
|
|
||||||
}
|
|
||||||
|
|
||||||
} else {
|
|
||||||
LOG_MESSAGE(QString("llama.cpp message marked as complete for %1").arg(requestId));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void LlamaCppProvider::cleanupRequest(const LLMCore::RequestID &requestId)
|
|
||||||
{
|
|
||||||
LOG_MESSAGE(QString("Cleaning up llama.cpp request %1").arg(requestId));
|
|
||||||
|
|
||||||
if (m_messages.contains(requestId)) {
|
|
||||||
OpenAIMessage *message = m_messages.take(requestId);
|
|
||||||
message->deleteLater();
|
|
||||||
}
|
|
||||||
|
|
||||||
m_dataBuffers.remove(requestId);
|
|
||||||
m_requestUrls.remove(requestId);
|
|
||||||
m_originalRequests.remove(requestId);
|
|
||||||
m_toolsManager->cleanupRequest(requestId);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace QodeAssist::Providers
|
} // namespace QodeAssist::Providers
|
||||||
|
|||||||
@@ -19,13 +19,13 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "OpenAIMessage.hpp"
|
#include <pluginllmcore/Provider.hpp>
|
||||||
#include "tools/ToolsManager.hpp"
|
|
||||||
#include <llmcore/Provider.hpp>
|
#include <LLMCore/LlamaCppClient.hpp>
|
||||||
|
|
||||||
namespace QodeAssist::Providers {
|
namespace QodeAssist::Providers {
|
||||||
|
|
||||||
class LlamaCppProvider : public LLMCore::Provider
|
class LlamaCppProvider : public PluginLLMCore::Provider
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
public:
|
public:
|
||||||
@@ -35,47 +35,22 @@ public:
|
|||||||
QString url() const override;
|
QString url() const override;
|
||||||
QString completionEndpoint() const override;
|
QString completionEndpoint() const override;
|
||||||
QString chatEndpoint() const override;
|
QString chatEndpoint() const override;
|
||||||
bool supportsModelListing() const override;
|
|
||||||
void prepareRequest(
|
void prepareRequest(
|
||||||
QJsonObject &request,
|
QJsonObject &request,
|
||||||
LLMCore::PromptTemplate *prompt,
|
PluginLLMCore::PromptTemplate *prompt,
|
||||||
LLMCore::ContextData context,
|
PluginLLMCore::ContextData context,
|
||||||
LLMCore::RequestType type,
|
PluginLLMCore::RequestType type,
|
||||||
bool isToolsEnabled,
|
bool isToolsEnabled,
|
||||||
bool isThinkingEnabled) override;
|
bool isThinkingEnabled) override;
|
||||||
QFuture<QList<QString>> getInstalledModels(const QString &url) override;
|
QFuture<QList<QString>> getInstalledModels(const QString &url) override;
|
||||||
QList<QString> validateRequest(const QJsonObject &request, LLMCore::TemplateType type) override;
|
PluginLLMCore::ProviderID providerID() const override;
|
||||||
|
PluginLLMCore::ProviderCapabilities capabilities() const override;
|
||||||
|
|
||||||
|
::LLMCore::BaseClient *client() const override;
|
||||||
QString apiKey() const override;
|
QString apiKey() const override;
|
||||||
void prepareNetworkRequest(QNetworkRequest &networkRequest) const override;
|
|
||||||
LLMCore::ProviderID providerID() const override;
|
|
||||||
|
|
||||||
void sendRequest(
|
|
||||||
const LLMCore::RequestID &requestId, const QUrl &url, const QJsonObject &payload) override;
|
|
||||||
|
|
||||||
bool supportsTools() const override;
|
|
||||||
bool supportImage() const override;
|
|
||||||
void cancelRequest(const LLMCore::RequestID &requestId) override;
|
|
||||||
|
|
||||||
public slots:
|
|
||||||
void onDataReceived(
|
|
||||||
const QodeAssist::LLMCore::RequestID &requestId, const QByteArray &data) override;
|
|
||||||
void onRequestFinished(
|
|
||||||
const QodeAssist::LLMCore::RequestID &requestId,
|
|
||||||
std::optional<QString> error) override;
|
|
||||||
|
|
||||||
private slots:
|
|
||||||
void onToolExecutionComplete(
|
|
||||||
const QString &requestId, const QHash<QString, QString> &toolResults);
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void processStreamChunk(const QString &requestId, const QJsonObject &chunk);
|
::LLMCore::LlamaCppClient *m_client;
|
||||||
void handleMessageComplete(const QString &requestId);
|
|
||||||
void cleanupRequest(const LLMCore::RequestID &requestId);
|
|
||||||
|
|
||||||
QHash<LLMCore::RequestID, OpenAIMessage *> m_messages;
|
|
||||||
QHash<LLMCore::RequestID, QUrl> m_requestUrls;
|
|
||||||
QHash<LLMCore::RequestID, QJsonObject> m_originalRequests;
|
|
||||||
Tools::ToolsManager *m_toolsManager;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace QodeAssist::Providers
|
} // namespace QodeAssist::Providers
|
||||||
|
|||||||
@@ -19,13 +19,14 @@
|
|||||||
|
|
||||||
#include "MistralAIProvider.hpp"
|
#include "MistralAIProvider.hpp"
|
||||||
|
|
||||||
#include "llmcore/ValidationUtils.hpp"
|
#include <LLMCore/ToolsManager.hpp>
|
||||||
#include "logger/Logger.hpp"
|
#include "logger/Logger.hpp"
|
||||||
#include "settings/ChatAssistantSettings.hpp"
|
#include "settings/ChatAssistantSettings.hpp"
|
||||||
#include "settings/CodeCompletionSettings.hpp"
|
#include "settings/CodeCompletionSettings.hpp"
|
||||||
#include "settings/QuickRefactorSettings.hpp"
|
#include "settings/QuickRefactorSettings.hpp"
|
||||||
#include "settings/GeneralSettings.hpp"
|
#include "settings/GeneralSettings.hpp"
|
||||||
#include "settings/ProviderSettings.hpp"
|
#include "settings/ProviderSettings.hpp"
|
||||||
|
#include "tools/ToolsRegistration.hpp"
|
||||||
|
|
||||||
#include <QJsonArray>
|
#include <QJsonArray>
|
||||||
#include <QJsonDocument>
|
#include <QJsonDocument>
|
||||||
@@ -34,14 +35,10 @@
|
|||||||
namespace QodeAssist::Providers {
|
namespace QodeAssist::Providers {
|
||||||
|
|
||||||
MistralAIProvider::MistralAIProvider(QObject *parent)
|
MistralAIProvider::MistralAIProvider(QObject *parent)
|
||||||
: LLMCore::Provider(parent)
|
: PluginLLMCore::Provider(parent)
|
||||||
, m_toolsManager(new Tools::ToolsManager(this))
|
, m_client(new ::LLMCore::OpenAIClient(QString(), QString(), QString(), this))
|
||||||
{
|
{
|
||||||
connect(
|
Tools::registerQodeAssistTools(m_client->tools());
|
||||||
m_toolsManager,
|
|
||||||
&Tools::ToolsManager::toolExecutionComplete,
|
|
||||||
this,
|
|
||||||
&MistralAIProvider::onToolExecutionComplete);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
QString MistralAIProvider::name() const
|
QString MistralAIProvider::name() const
|
||||||
@@ -49,6 +46,11 @@ QString MistralAIProvider::name() const
|
|||||||
return "Mistral AI";
|
return "Mistral AI";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QString MistralAIProvider::apiKey() const
|
||||||
|
{
|
||||||
|
return Settings::providerSettings().mistralAiApiKey();
|
||||||
|
}
|
||||||
|
|
||||||
QString MistralAIProvider::url() const
|
QString MistralAIProvider::url() const
|
||||||
{
|
{
|
||||||
return "https://api.mistral.ai";
|
return "https://api.mistral.ai";
|
||||||
@@ -64,176 +66,29 @@ QString MistralAIProvider::chatEndpoint() const
|
|||||||
return "/v1/chat/completions";
|
return "/v1/chat/completions";
|
||||||
}
|
}
|
||||||
|
|
||||||
bool MistralAIProvider::supportsModelListing() const
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
QFuture<QList<QString>> MistralAIProvider::getInstalledModels(const QString &url)
|
QFuture<QList<QString>> MistralAIProvider::getInstalledModels(const QString &url)
|
||||||
{
|
{
|
||||||
QNetworkRequest request(QString("%1/v1/models").arg(url));
|
m_client->setUrl(url);
|
||||||
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
|
m_client->setApiKey(apiKey());
|
||||||
if (!apiKey().isEmpty()) {
|
return m_client->listModels();
|
||||||
request.setRawHeader("Authorization", QString("Bearer %1").arg(apiKey()).toUtf8());
|
|
||||||
}
|
|
||||||
|
|
||||||
return httpClient()->get(request).then([](const QByteArray &data) {
|
|
||||||
QList<QString> models;
|
|
||||||
QJsonObject jsonObject = QJsonDocument::fromJson(data).object();
|
|
||||||
|
|
||||||
if (jsonObject.contains("data") && jsonObject["object"].toString() == "list") {
|
|
||||||
QJsonArray modelArray = jsonObject["data"].toArray();
|
|
||||||
for (const QJsonValue &value : modelArray) {
|
|
||||||
QJsonObject modelObject = value.toObject();
|
|
||||||
if (modelObject.contains("id")) {
|
|
||||||
models.append(modelObject["id"].toString());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return models;
|
|
||||||
}).onFailed([](const std::exception &e) {
|
|
||||||
LOG_MESSAGE(QString("Error fetching Mistral AI models: %1").arg(e.what()));
|
|
||||||
return QList<QString>{};
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
QList<QString> MistralAIProvider::validateRequest(
|
PluginLLMCore::ProviderID MistralAIProvider::providerID() const
|
||||||
const QJsonObject &request, LLMCore::TemplateType type)
|
|
||||||
{
|
{
|
||||||
const auto fimReq = QJsonObject{
|
return PluginLLMCore::ProviderID::MistralAI;
|
||||||
{"model", {}},
|
|
||||||
{"max_tokens", {}},
|
|
||||||
{"stream", {}},
|
|
||||||
{"temperature", {}},
|
|
||||||
{"prompt", {}},
|
|
||||||
{"suffix", {}}};
|
|
||||||
|
|
||||||
const auto templateReq = QJsonObject{
|
|
||||||
{"model", {}},
|
|
||||||
{"messages", QJsonArray{{QJsonObject{{"role", {}}, {"content", {}}}}}},
|
|
||||||
{"temperature", {}},
|
|
||||||
{"max_tokens", {}},
|
|
||||||
{"top_p", {}},
|
|
||||||
{"top_k", {}},
|
|
||||||
{"frequency_penalty", {}},
|
|
||||||
{"presence_penalty", {}},
|
|
||||||
{"stop", QJsonArray{}},
|
|
||||||
{"stream", {}},
|
|
||||||
{"tools", {}}};
|
|
||||||
|
|
||||||
return LLMCore::ValidationUtils::validateRequestFields(
|
|
||||||
request, type == LLMCore::TemplateType::FIM ? fimReq : templateReq);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
QString MistralAIProvider::apiKey() const
|
PluginLLMCore::ProviderCapabilities MistralAIProvider::capabilities() const
|
||||||
{
|
{
|
||||||
return Settings::providerSettings().mistralAiApiKey();
|
return PluginLLMCore::ProviderCapability::Tools | PluginLLMCore::ProviderCapability::Image
|
||||||
}
|
| PluginLLMCore::ProviderCapability::ModelListing;
|
||||||
|
|
||||||
void MistralAIProvider::prepareNetworkRequest(QNetworkRequest &networkRequest) const
|
|
||||||
{
|
|
||||||
networkRequest.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
|
|
||||||
|
|
||||||
if (!apiKey().isEmpty()) {
|
|
||||||
networkRequest.setRawHeader("Authorization", QString("Bearer %1").arg(apiKey()).toUtf8());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
LLMCore::ProviderID MistralAIProvider::providerID() const
|
|
||||||
{
|
|
||||||
return LLMCore::ProviderID::MistralAI;
|
|
||||||
}
|
|
||||||
|
|
||||||
void MistralAIProvider::sendRequest(
|
|
||||||
const LLMCore::RequestID &requestId, const QUrl &url, const QJsonObject &payload)
|
|
||||||
{
|
|
||||||
if (!m_messages.contains(requestId)) {
|
|
||||||
m_dataBuffers[requestId].clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
m_requestUrls[requestId] = url;
|
|
||||||
m_originalRequests[requestId] = payload;
|
|
||||||
|
|
||||||
QNetworkRequest networkRequest(url);
|
|
||||||
prepareNetworkRequest(networkRequest);
|
|
||||||
|
|
||||||
LOG_MESSAGE(
|
|
||||||
QString("MistralAIProvider: Sending request %1 to %2").arg(requestId, url.toString()));
|
|
||||||
|
|
||||||
httpClient()->postStreaming(requestId, networkRequest, payload);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool MistralAIProvider::supportsTools() const
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool MistralAIProvider::supportImage() const
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
void MistralAIProvider::cancelRequest(const LLMCore::RequestID &requestId)
|
|
||||||
{
|
|
||||||
LOG_MESSAGE(QString("MistralAIProvider: Cancelling request %1").arg(requestId));
|
|
||||||
LLMCore::Provider::cancelRequest(requestId);
|
|
||||||
cleanupRequest(requestId);
|
|
||||||
}
|
|
||||||
|
|
||||||
void MistralAIProvider::onDataReceived(
|
|
||||||
const QodeAssist::LLMCore::RequestID &requestId, const QByteArray &data)
|
|
||||||
{
|
|
||||||
LLMCore::DataBuffers &buffers = m_dataBuffers[requestId];
|
|
||||||
QStringList lines = buffers.rawStreamBuffer.processData(data);
|
|
||||||
|
|
||||||
for (const QString &line : lines) {
|
|
||||||
if (line.trimmed().isEmpty() || line == "data: [DONE]") {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
QJsonObject chunk = parseEventLine(line);
|
|
||||||
if (chunk.isEmpty())
|
|
||||||
continue;
|
|
||||||
|
|
||||||
processStreamChunk(requestId, chunk);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void MistralAIProvider::onRequestFinished(
|
|
||||||
const QodeAssist::LLMCore::RequestID &requestId, std::optional<QString> error)
|
|
||||||
{
|
|
||||||
if (error) {
|
|
||||||
LOG_MESSAGE(QString("MistralAIProvider request %1 failed: %2").arg(requestId, *error));
|
|
||||||
emit requestFailed(requestId, *error);
|
|
||||||
cleanupRequest(requestId);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (m_messages.contains(requestId)) {
|
|
||||||
OpenAIMessage *message = m_messages[requestId];
|
|
||||||
if (message->state() == LLMCore::MessageState::RequiresToolExecution) {
|
|
||||||
LOG_MESSAGE(QString("Waiting for tools to complete for %1").arg(requestId));
|
|
||||||
m_dataBuffers.remove(requestId);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (m_dataBuffers.contains(requestId)) {
|
|
||||||
const LLMCore::DataBuffers &buffers = m_dataBuffers[requestId];
|
|
||||||
if (!buffers.responseContent.isEmpty()) {
|
|
||||||
LOG_MESSAGE(QString("Emitting full response for %1").arg(requestId));
|
|
||||||
emit fullResponseReceived(requestId, buffers.responseContent);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
cleanupRequest(requestId);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void MistralAIProvider::prepareRequest(
|
void MistralAIProvider::prepareRequest(
|
||||||
QJsonObject &request,
|
QJsonObject &request,
|
||||||
LLMCore::PromptTemplate *prompt,
|
PluginLLMCore::PromptTemplate *prompt,
|
||||||
LLMCore::ContextData context,
|
PluginLLMCore::ContextData context,
|
||||||
LLMCore::RequestType type,
|
PluginLLMCore::RequestType type,
|
||||||
bool isToolsEnabled,
|
bool isToolsEnabled,
|
||||||
bool isThinkingEnabled)
|
bool isThinkingEnabled)
|
||||||
{
|
{
|
||||||
@@ -257,22 +112,16 @@ void MistralAIProvider::prepareRequest(
|
|||||||
request["presence_penalty"] = settings.presencePenalty();
|
request["presence_penalty"] = settings.presencePenalty();
|
||||||
};
|
};
|
||||||
|
|
||||||
if (type == LLMCore::RequestType::CodeCompletion) {
|
if (type == PluginLLMCore::RequestType::CodeCompletion) {
|
||||||
applyModelParams(Settings::codeCompletionSettings());
|
applyModelParams(Settings::codeCompletionSettings());
|
||||||
} else if (type == LLMCore::RequestType::QuickRefactoring) {
|
} else if (type == PluginLLMCore::RequestType::QuickRefactoring) {
|
||||||
applyModelParams(Settings::quickRefactorSettings());
|
applyModelParams(Settings::quickRefactorSettings());
|
||||||
} else {
|
} else {
|
||||||
applyModelParams(Settings::chatAssistantSettings());
|
applyModelParams(Settings::chatAssistantSettings());
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isToolsEnabled) {
|
if (isToolsEnabled) {
|
||||||
LLMCore::RunToolsFilter filter = LLMCore::RunToolsFilter::ALL;
|
auto toolsDefinitions = m_client->tools()->getToolsDefinitions();
|
||||||
if (type == LLMCore::RequestType::QuickRefactoring) {
|
|
||||||
filter = LLMCore::RunToolsFilter::OnlyRead;
|
|
||||||
}
|
|
||||||
|
|
||||||
auto toolsDefinitions = m_toolsManager->getToolsDefinitions(
|
|
||||||
LLMCore::ToolSchemaFormat::OpenAI, filter);
|
|
||||||
if (!toolsDefinitions.isEmpty()) {
|
if (!toolsDefinitions.isEmpty()) {
|
||||||
request["tools"] = toolsDefinitions;
|
request["tools"] = toolsDefinitions;
|
||||||
LOG_MESSAGE(QString("Added %1 tools to Mistral request").arg(toolsDefinitions.size()));
|
LOG_MESSAGE(QString("Added %1 tools to Mistral request").arg(toolsDefinitions.size()));
|
||||||
@@ -280,163 +129,9 @@ void MistralAIProvider::prepareRequest(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void MistralAIProvider::onToolExecutionComplete(
|
::LLMCore::BaseClient *MistralAIProvider::client() const
|
||||||
const QString &requestId, const QHash<QString, QString> &toolResults)
|
|
||||||
{
|
{
|
||||||
if (!m_messages.contains(requestId) || !m_requestUrls.contains(requestId)) {
|
return m_client;
|
||||||
LOG_MESSAGE(QString("ERROR: Missing data for continuation request %1").arg(requestId));
|
|
||||||
cleanupRequest(requestId);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
LOG_MESSAGE(QString("Tool execution complete for Mistral request %1").arg(requestId));
|
|
||||||
|
|
||||||
for (auto it = toolResults.begin(); it != toolResults.end(); ++it) {
|
|
||||||
OpenAIMessage *message = m_messages[requestId];
|
|
||||||
auto toolContent = message->getCurrentToolUseContent();
|
|
||||||
for (auto tool : toolContent) {
|
|
||||||
if (tool->id() == it.key()) {
|
|
||||||
auto toolStringName = m_toolsManager->toolsFactory()->getStringName(tool->name());
|
|
||||||
emit toolExecutionCompleted(
|
|
||||||
requestId, tool->id(), toolStringName, toolResults[tool->id()]);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
OpenAIMessage *message = m_messages[requestId];
|
|
||||||
QJsonObject continuationRequest = m_originalRequests[requestId];
|
|
||||||
QJsonArray messages = continuationRequest["messages"].toArray();
|
|
||||||
|
|
||||||
messages.append(message->toProviderFormat());
|
|
||||||
|
|
||||||
QJsonArray toolResultMessages = message->createToolResultMessages(toolResults);
|
|
||||||
for (const auto &toolMsg : toolResultMessages) {
|
|
||||||
messages.append(toolMsg);
|
|
||||||
}
|
|
||||||
|
|
||||||
continuationRequest["messages"] = messages;
|
|
||||||
|
|
||||||
LOG_MESSAGE(QString("Sending continuation request for %1 with %2 tool results")
|
|
||||||
.arg(requestId)
|
|
||||||
.arg(toolResults.size()));
|
|
||||||
|
|
||||||
sendRequest(requestId, m_requestUrls[requestId], continuationRequest);
|
|
||||||
}
|
|
||||||
|
|
||||||
void MistralAIProvider::processStreamChunk(const QString &requestId, const QJsonObject &chunk)
|
|
||||||
{
|
|
||||||
QJsonArray choices = chunk["choices"].toArray();
|
|
||||||
if (choices.isEmpty()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
QJsonObject choice = choices[0].toObject();
|
|
||||||
QJsonObject delta = choice["delta"].toObject();
|
|
||||||
QString finishReason = choice["finish_reason"].toString();
|
|
||||||
|
|
||||||
OpenAIMessage *message = m_messages.value(requestId);
|
|
||||||
if (!message) {
|
|
||||||
message = new OpenAIMessage(this);
|
|
||||||
m_messages[requestId] = message;
|
|
||||||
LOG_MESSAGE(QString("Created NEW OpenAIMessage for Mistral request %1").arg(requestId));
|
|
||||||
|
|
||||||
if (m_dataBuffers.contains(requestId)) {
|
|
||||||
emit continuationStarted(requestId);
|
|
||||||
LOG_MESSAGE(QString("Starting continuation for request %1").arg(requestId));
|
|
||||||
}
|
|
||||||
} else if (
|
|
||||||
m_dataBuffers.contains(requestId)
|
|
||||||
&& message->state() == LLMCore::MessageState::RequiresToolExecution) {
|
|
||||||
message->startNewContinuation();
|
|
||||||
emit continuationStarted(requestId);
|
|
||||||
LOG_MESSAGE(QString("Cleared message state for continuation request %1").arg(requestId));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (delta.contains("content") && !delta["content"].isNull()) {
|
|
||||||
QString content = delta["content"].toString();
|
|
||||||
message->handleContentDelta(content);
|
|
||||||
|
|
||||||
LLMCore::DataBuffers &buffers = m_dataBuffers[requestId];
|
|
||||||
buffers.responseContent += content;
|
|
||||||
emit partialResponseReceived(requestId, content);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (delta.contains("tool_calls")) {
|
|
||||||
QJsonArray toolCalls = delta["tool_calls"].toArray();
|
|
||||||
for (const auto &toolCallValue : toolCalls) {
|
|
||||||
QJsonObject toolCall = toolCallValue.toObject();
|
|
||||||
int index = toolCall["index"].toInt();
|
|
||||||
|
|
||||||
if (toolCall.contains("id")) {
|
|
||||||
QString id = toolCall["id"].toString();
|
|
||||||
QJsonObject function = toolCall["function"].toObject();
|
|
||||||
QString name = function["name"].toString();
|
|
||||||
message->handleToolCallStart(index, id, name);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (toolCall.contains("function")) {
|
|
||||||
QJsonObject function = toolCall["function"].toObject();
|
|
||||||
if (function.contains("arguments")) {
|
|
||||||
QString args = function["arguments"].toString();
|
|
||||||
message->handleToolCallDelta(index, args);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!finishReason.isEmpty() && finishReason != "null") {
|
|
||||||
for (int i = 0; i < 10; ++i) {
|
|
||||||
message->handleToolCallComplete(i);
|
|
||||||
}
|
|
||||||
|
|
||||||
message->handleFinishReason(finishReason);
|
|
||||||
handleMessageComplete(requestId);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void MistralAIProvider::handleMessageComplete(const QString &requestId)
|
|
||||||
{
|
|
||||||
if (!m_messages.contains(requestId))
|
|
||||||
return;
|
|
||||||
|
|
||||||
OpenAIMessage *message = m_messages[requestId];
|
|
||||||
|
|
||||||
if (message->state() == LLMCore::MessageState::RequiresToolExecution) {
|
|
||||||
LOG_MESSAGE(QString("Mistral message requires tool execution for %1").arg(requestId));
|
|
||||||
|
|
||||||
auto toolUseContent = message->getCurrentToolUseContent();
|
|
||||||
|
|
||||||
if (toolUseContent.isEmpty()) {
|
|
||||||
LOG_MESSAGE(QString("No tools to execute for %1").arg(requestId));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (auto toolContent : toolUseContent) {
|
|
||||||
auto toolStringName = m_toolsManager->toolsFactory()->getStringName(toolContent->name());
|
|
||||||
emit toolExecutionStarted(requestId, toolContent->id(), toolStringName);
|
|
||||||
m_toolsManager->executeToolCall(
|
|
||||||
requestId, toolContent->id(), toolContent->name(), toolContent->input());
|
|
||||||
}
|
|
||||||
|
|
||||||
} else {
|
|
||||||
LOG_MESSAGE(QString("Mistral message marked as complete for %1").arg(requestId));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void MistralAIProvider::cleanupRequest(const LLMCore::RequestID &requestId)
|
|
||||||
{
|
|
||||||
LOG_MESSAGE(QString("Cleaning up Mistral request %1").arg(requestId));
|
|
||||||
|
|
||||||
if (m_messages.contains(requestId)) {
|
|
||||||
OpenAIMessage *message = m_messages.take(requestId);
|
|
||||||
message->deleteLater();
|
|
||||||
}
|
|
||||||
|
|
||||||
m_dataBuffers.remove(requestId);
|
|
||||||
m_requestUrls.remove(requestId);
|
|
||||||
m_originalRequests.remove(requestId);
|
|
||||||
m_toolsManager->cleanupRequest(requestId);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace QodeAssist::Providers
|
} // namespace QodeAssist::Providers
|
||||||
|
|||||||
@@ -19,13 +19,12 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "OpenAIMessage.hpp"
|
#include <LLMCore/OpenAIClient.hpp>
|
||||||
#include "tools/ToolsManager.hpp"
|
#include <pluginllmcore/Provider.hpp>
|
||||||
#include <llmcore/Provider.hpp>
|
|
||||||
|
|
||||||
namespace QodeAssist::Providers {
|
namespace QodeAssist::Providers {
|
||||||
|
|
||||||
class MistralAIProvider : public LLMCore::Provider
|
class MistralAIProvider : public PluginLLMCore::Provider
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
public:
|
public:
|
||||||
@@ -35,47 +34,22 @@ public:
|
|||||||
QString url() const override;
|
QString url() const override;
|
||||||
QString completionEndpoint() const override;
|
QString completionEndpoint() const override;
|
||||||
QString chatEndpoint() const override;
|
QString chatEndpoint() const override;
|
||||||
bool supportsModelListing() const override;
|
|
||||||
void prepareRequest(
|
void prepareRequest(
|
||||||
QJsonObject &request,
|
QJsonObject &request,
|
||||||
LLMCore::PromptTemplate *prompt,
|
PluginLLMCore::PromptTemplate *prompt,
|
||||||
LLMCore::ContextData context,
|
PluginLLMCore::ContextData context,
|
||||||
LLMCore::RequestType type,
|
PluginLLMCore::RequestType type,
|
||||||
bool isToolsEnabled,
|
bool isToolsEnabled,
|
||||||
bool isThinkingEnabled) override;
|
bool isThinkingEnabled) override;
|
||||||
QFuture<QList<QString>> getInstalledModels(const QString &url) override;
|
QFuture<QList<QString>> getInstalledModels(const QString &url) override;
|
||||||
QList<QString> validateRequest(const QJsonObject &request, LLMCore::TemplateType type) override;
|
PluginLLMCore::ProviderID providerID() const override;
|
||||||
|
PluginLLMCore::ProviderCapabilities capabilities() const override;
|
||||||
|
|
||||||
|
::LLMCore::BaseClient *client() const override;
|
||||||
QString apiKey() const override;
|
QString apiKey() const override;
|
||||||
void prepareNetworkRequest(QNetworkRequest &networkRequest) const override;
|
|
||||||
LLMCore::ProviderID providerID() const override;
|
|
||||||
|
|
||||||
void sendRequest(
|
|
||||||
const LLMCore::RequestID &requestId, const QUrl &url, const QJsonObject &payload) override;
|
|
||||||
|
|
||||||
bool supportsTools() const override;
|
|
||||||
bool supportImage() const override;
|
|
||||||
void cancelRequest(const LLMCore::RequestID &requestId) override;
|
|
||||||
|
|
||||||
public slots:
|
|
||||||
void onDataReceived(
|
|
||||||
const QodeAssist::LLMCore::RequestID &requestId, const QByteArray &data) override;
|
|
||||||
void onRequestFinished(
|
|
||||||
const QodeAssist::LLMCore::RequestID &requestId,
|
|
||||||
std::optional<QString> error) override;
|
|
||||||
|
|
||||||
private slots:
|
|
||||||
void onToolExecutionComplete(
|
|
||||||
const QString &requestId, const QHash<QString, QString> &toolResults);
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void processStreamChunk(const QString &requestId, const QJsonObject &chunk);
|
::LLMCore::OpenAIClient *m_client;
|
||||||
void handleMessageComplete(const QString &requestId);
|
|
||||||
void cleanupRequest(const LLMCore::RequestID &requestId);
|
|
||||||
|
|
||||||
QHash<LLMCore::RequestID, OpenAIMessage *> m_messages;
|
|
||||||
QHash<LLMCore::RequestID, QUrl> m_requestUrls;
|
|
||||||
QHash<LLMCore::RequestID, QJsonObject> m_originalRequests;
|
|
||||||
Tools::ToolsManager *m_toolsManager;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace QodeAssist::Providers
|
} // namespace QodeAssist::Providers
|
||||||
|
|||||||
@@ -39,13 +39,13 @@ void OllamaMessage::handleContentDelta(const QString &content)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!m_contentAddedToTextBlock) {
|
if (!m_contentAddedToTextBlock) {
|
||||||
LLMCore::TextContent *textContent = getOrCreateTextContent();
|
PluginLLMCore::TextContent *textContent = getOrCreateTextContent();
|
||||||
textContent->setText(m_accumulatedContent);
|
textContent->setText(m_accumulatedContent);
|
||||||
m_contentAddedToTextBlock = true;
|
m_contentAddedToTextBlock = true;
|
||||||
LOG_MESSAGE(QString("OllamaMessage: Added accumulated content to TextContent, length=%1")
|
LOG_MESSAGE(QString("OllamaMessage: Added accumulated content to TextContent, length=%1")
|
||||||
.arg(m_accumulatedContent.length()));
|
.arg(m_accumulatedContent.length()));
|
||||||
} else {
|
} else {
|
||||||
LLMCore::TextContent *textContent = getOrCreateTextContent();
|
PluginLLMCore::TextContent *textContent = getOrCreateTextContent();
|
||||||
textContent->appendText(content);
|
textContent->appendText(content);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -65,7 +65,7 @@ void OllamaMessage::handleToolCall(const QJsonObject &toolCall)
|
|||||||
m_accumulatedContent.clear();
|
m_accumulatedContent.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
addCurrentContent<LLMCore::ToolUseContent>(toolId, name, arguments);
|
addCurrentContent<PluginLLMCore::ToolUseContent>(toolId, name, arguments);
|
||||||
|
|
||||||
LOG_MESSAGE(
|
LOG_MESSAGE(
|
||||||
QString("OllamaMessage: Structured tool call detected - name=%1, id=%2").arg(name, toolId));
|
QString("OllamaMessage: Structured tool call detected - name=%1, id=%2").arg(name, toolId));
|
||||||
@@ -73,7 +73,7 @@ void OllamaMessage::handleToolCall(const QJsonObject &toolCall)
|
|||||||
|
|
||||||
void OllamaMessage::handleThinkingDelta(const QString &thinking)
|
void OllamaMessage::handleThinkingDelta(const QString &thinking)
|
||||||
{
|
{
|
||||||
LLMCore::ThinkingContent *thinkingContent = getOrCreateThinkingContent();
|
PluginLLMCore::ThinkingContent *thinkingContent = getOrCreateThinkingContent();
|
||||||
thinkingContent->appendThinking(thinking);
|
thinkingContent->appendThinking(thinking);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -102,7 +102,7 @@ void OllamaMessage::handleDone(bool done)
|
|||||||
.arg(trimmed.length()));
|
.arg(trimmed.length()));
|
||||||
|
|
||||||
for (auto it = m_currentBlocks.begin(); it != m_currentBlocks.end();) {
|
for (auto it = m_currentBlocks.begin(); it != m_currentBlocks.end();) {
|
||||||
if (qobject_cast<LLMCore::TextContent *>(*it)) {
|
if (qobject_cast<PluginLLMCore::TextContent *>(*it)) {
|
||||||
LOG_MESSAGE(QString(
|
LOG_MESSAGE(QString(
|
||||||
"OllamaMessage: Removing TextContent block (incomplete tool call)"));
|
"OllamaMessage: Removing TextContent block (incomplete tool call)"));
|
||||||
(*it)->deleteLater();
|
(*it)->deleteLater();
|
||||||
@@ -114,7 +114,7 @@ void OllamaMessage::handleDone(bool done)
|
|||||||
|
|
||||||
m_accumulatedContent.clear();
|
m_accumulatedContent.clear();
|
||||||
} else {
|
} else {
|
||||||
LLMCore::TextContent *textContent = getOrCreateTextContent();
|
PluginLLMCore::TextContent *textContent = getOrCreateTextContent();
|
||||||
textContent->setText(m_accumulatedContent);
|
textContent->setText(m_accumulatedContent);
|
||||||
m_contentAddedToTextBlock = true;
|
m_contentAddedToTextBlock = true;
|
||||||
LOG_MESSAGE(
|
LOG_MESSAGE(
|
||||||
@@ -184,13 +184,13 @@ bool OllamaMessage::tryParseToolCall()
|
|||||||
QString toolId = QString("call_%1_%2").arg(name).arg(QDateTime::currentMSecsSinceEpoch());
|
QString toolId = QString("call_%1_%2").arg(name).arg(QDateTime::currentMSecsSinceEpoch());
|
||||||
|
|
||||||
for (auto block : m_currentBlocks) {
|
for (auto block : m_currentBlocks) {
|
||||||
if (qobject_cast<LLMCore::TextContent *>(block)) {
|
if (qobject_cast<PluginLLMCore::TextContent *>(block)) {
|
||||||
LOG_MESSAGE(QString("OllamaMessage: Removing TextContent block (tool call detected)"));
|
LOG_MESSAGE(QString("OllamaMessage: Removing TextContent block (tool call detected)"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
m_currentBlocks.clear();
|
m_currentBlocks.clear();
|
||||||
|
|
||||||
addCurrentContent<LLMCore::ToolUseContent>(toolId, name, arguments);
|
addCurrentContent<PluginLLMCore::ToolUseContent>(toolId, name, arguments);
|
||||||
|
|
||||||
LOG_MESSAGE(
|
LOG_MESSAGE(
|
||||||
QString(
|
QString(
|
||||||
@@ -238,14 +238,14 @@ QJsonObject OllamaMessage::toProviderFormat() const
|
|||||||
if (!block)
|
if (!block)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
if (auto text = qobject_cast<LLMCore::TextContent *>(block)) {
|
if (auto text = qobject_cast<PluginLLMCore::TextContent *>(block)) {
|
||||||
textContent += text->text();
|
textContent += text->text();
|
||||||
} else if (auto tool = qobject_cast<LLMCore::ToolUseContent *>(block)) {
|
} else if (auto tool = qobject_cast<PluginLLMCore::ToolUseContent *>(block)) {
|
||||||
QJsonObject toolCall;
|
QJsonObject toolCall;
|
||||||
toolCall["type"] = "function";
|
toolCall["type"] = "function";
|
||||||
toolCall["function"] = QJsonObject{{"name", tool->name()}, {"arguments", tool->input()}};
|
toolCall["function"] = QJsonObject{{"name", tool->name()}, {"arguments", tool->input()}};
|
||||||
toolCalls.append(toolCall);
|
toolCalls.append(toolCall);
|
||||||
} else if (auto thinking = qobject_cast<LLMCore::ThinkingContent *>(block)) {
|
} else if (auto thinking = qobject_cast<PluginLLMCore::ThinkingContent *>(block)) {
|
||||||
thinkingContent += thinking->thinking();
|
thinkingContent += thinking->thinking();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -287,22 +287,22 @@ QJsonArray OllamaMessage::createToolResultMessages(const QHash<QString, QString>
|
|||||||
return messages;
|
return messages;
|
||||||
}
|
}
|
||||||
|
|
||||||
QList<LLMCore::ToolUseContent *> OllamaMessage::getCurrentToolUseContent() const
|
QList<PluginLLMCore::ToolUseContent *> OllamaMessage::getCurrentToolUseContent() const
|
||||||
{
|
{
|
||||||
QList<LLMCore::ToolUseContent *> toolBlocks;
|
QList<PluginLLMCore::ToolUseContent *> toolBlocks;
|
||||||
for (auto block : m_currentBlocks) {
|
for (auto block : m_currentBlocks) {
|
||||||
if (auto toolContent = qobject_cast<LLMCore::ToolUseContent *>(block)) {
|
if (auto toolContent = qobject_cast<PluginLLMCore::ToolUseContent *>(block)) {
|
||||||
toolBlocks.append(toolContent);
|
toolBlocks.append(toolContent);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return toolBlocks;
|
return toolBlocks;
|
||||||
}
|
}
|
||||||
|
|
||||||
QList<LLMCore::ThinkingContent *> OllamaMessage::getCurrentThinkingContent() const
|
QList<PluginLLMCore::ThinkingContent *> OllamaMessage::getCurrentThinkingContent() const
|
||||||
{
|
{
|
||||||
QList<LLMCore::ThinkingContent *> thinkingBlocks;
|
QList<PluginLLMCore::ThinkingContent *> thinkingBlocks;
|
||||||
for (auto block : m_currentBlocks) {
|
for (auto block : m_currentBlocks) {
|
||||||
if (auto thinkingContent = qobject_cast<LLMCore::ThinkingContent *>(block)) {
|
if (auto thinkingContent = qobject_cast<PluginLLMCore::ThinkingContent *>(block)) {
|
||||||
thinkingBlocks.append(thinkingContent);
|
thinkingBlocks.append(thinkingContent);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -316,7 +316,7 @@ void OllamaMessage::startNewContinuation()
|
|||||||
m_currentBlocks.clear();
|
m_currentBlocks.clear();
|
||||||
m_accumulatedContent.clear();
|
m_accumulatedContent.clear();
|
||||||
m_done = false;
|
m_done = false;
|
||||||
m_state = LLMCore::MessageState::Building;
|
m_state = PluginLLMCore::MessageState::Building;
|
||||||
m_contentAddedToTextBlock = false;
|
m_contentAddedToTextBlock = false;
|
||||||
m_currentThinkingContent = nullptr;
|
m_currentThinkingContent = nullptr;
|
||||||
}
|
}
|
||||||
@@ -324,40 +324,40 @@ void OllamaMessage::startNewContinuation()
|
|||||||
void OllamaMessage::updateStateFromDone()
|
void OllamaMessage::updateStateFromDone()
|
||||||
{
|
{
|
||||||
if (!getCurrentToolUseContent().empty()) {
|
if (!getCurrentToolUseContent().empty()) {
|
||||||
m_state = LLMCore::MessageState::RequiresToolExecution;
|
m_state = PluginLLMCore::MessageState::RequiresToolExecution;
|
||||||
LOG_MESSAGE(QString("OllamaMessage: State set to RequiresToolExecution, tools count=%1")
|
LOG_MESSAGE(QString("OllamaMessage: State set to RequiresToolExecution, tools count=%1")
|
||||||
.arg(getCurrentToolUseContent().size()));
|
.arg(getCurrentToolUseContent().size()));
|
||||||
} else {
|
} else {
|
||||||
m_state = LLMCore::MessageState::Final;
|
m_state = PluginLLMCore::MessageState::Final;
|
||||||
LOG_MESSAGE(QString("OllamaMessage: State set to Final"));
|
LOG_MESSAGE(QString("OllamaMessage: State set to Final"));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
LLMCore::TextContent *OllamaMessage::getOrCreateTextContent()
|
PluginLLMCore::TextContent *OllamaMessage::getOrCreateTextContent()
|
||||||
{
|
{
|
||||||
for (auto block : m_currentBlocks) {
|
for (auto block : m_currentBlocks) {
|
||||||
if (auto textContent = qobject_cast<LLMCore::TextContent *>(block)) {
|
if (auto textContent = qobject_cast<PluginLLMCore::TextContent *>(block)) {
|
||||||
return textContent;
|
return textContent;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return addCurrentContent<LLMCore::TextContent>();
|
return addCurrentContent<PluginLLMCore::TextContent>();
|
||||||
}
|
}
|
||||||
|
|
||||||
LLMCore::ThinkingContent *OllamaMessage::getOrCreateThinkingContent()
|
PluginLLMCore::ThinkingContent *OllamaMessage::getOrCreateThinkingContent()
|
||||||
{
|
{
|
||||||
if (m_currentThinkingContent) {
|
if (m_currentThinkingContent) {
|
||||||
return m_currentThinkingContent;
|
return m_currentThinkingContent;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (auto block : m_currentBlocks) {
|
for (auto block : m_currentBlocks) {
|
||||||
if (auto thinkingContent = qobject_cast<LLMCore::ThinkingContent *>(block)) {
|
if (auto thinkingContent = qobject_cast<PluginLLMCore::ThinkingContent *>(block)) {
|
||||||
m_currentThinkingContent = thinkingContent;
|
m_currentThinkingContent = thinkingContent;
|
||||||
return m_currentThinkingContent;
|
return m_currentThinkingContent;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
m_currentThinkingContent = addCurrentContent<LLMCore::ThinkingContent>();
|
m_currentThinkingContent = addCurrentContent<PluginLLMCore::ThinkingContent>();
|
||||||
LOG_MESSAGE(QString("OllamaMessage: Created new ThinkingContent block"));
|
LOG_MESSAGE(QString("OllamaMessage: Created new ThinkingContent block"));
|
||||||
return m_currentThinkingContent;
|
return m_currentThinkingContent;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,7 +19,7 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <llmcore/ContentBlocks.hpp>
|
#include <pluginllmcore/ContentBlocks.hpp>
|
||||||
|
|
||||||
namespace QodeAssist::Providers {
|
namespace QodeAssist::Providers {
|
||||||
|
|
||||||
@@ -38,26 +38,26 @@ public:
|
|||||||
QJsonObject toProviderFormat() const;
|
QJsonObject toProviderFormat() const;
|
||||||
QJsonArray createToolResultMessages(const QHash<QString, QString> &toolResults) const;
|
QJsonArray createToolResultMessages(const QHash<QString, QString> &toolResults) const;
|
||||||
|
|
||||||
LLMCore::MessageState state() const { return m_state; }
|
PluginLLMCore::MessageState state() const { return m_state; }
|
||||||
QList<LLMCore::ToolUseContent *> getCurrentToolUseContent() const;
|
QList<PluginLLMCore::ToolUseContent *> getCurrentToolUseContent() const;
|
||||||
QList<LLMCore::ThinkingContent *> getCurrentThinkingContent() const;
|
QList<PluginLLMCore::ThinkingContent *> getCurrentThinkingContent() const;
|
||||||
QList<LLMCore::ContentBlock *> currentBlocks() const { return m_currentBlocks; }
|
QList<PluginLLMCore::ContentBlock *> currentBlocks() const { return m_currentBlocks; }
|
||||||
|
|
||||||
void startNewContinuation();
|
void startNewContinuation();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool m_done = false;
|
bool m_done = false;
|
||||||
LLMCore::MessageState m_state = LLMCore::MessageState::Building;
|
PluginLLMCore::MessageState m_state = PluginLLMCore::MessageState::Building;
|
||||||
QList<LLMCore::ContentBlock *> m_currentBlocks;
|
QList<PluginLLMCore::ContentBlock *> m_currentBlocks;
|
||||||
QString m_accumulatedContent;
|
QString m_accumulatedContent;
|
||||||
bool m_contentAddedToTextBlock = false;
|
bool m_contentAddedToTextBlock = false;
|
||||||
LLMCore::ThinkingContent *m_currentThinkingContent = nullptr;
|
PluginLLMCore::ThinkingContent *m_currentThinkingContent = nullptr;
|
||||||
|
|
||||||
void updateStateFromDone();
|
void updateStateFromDone();
|
||||||
bool tryParseToolCall();
|
bool tryParseToolCall();
|
||||||
bool isLikelyToolCallJson(const QString &content) const;
|
bool isLikelyToolCallJson(const QString &content) const;
|
||||||
LLMCore::TextContent *getOrCreateTextContent();
|
PluginLLMCore::TextContent *getOrCreateTextContent();
|
||||||
LLMCore::ThinkingContent *getOrCreateThinkingContent();
|
PluginLLMCore::ThinkingContent *getOrCreateThinkingContent();
|
||||||
|
|
||||||
template<typename T, typename... Args>
|
template<typename T, typename... Args>
|
||||||
T *addCurrentContent(Args &&...args)
|
T *addCurrentContent(Args &&...args)
|
||||||
|
|||||||
@@ -19,29 +19,27 @@
|
|||||||
|
|
||||||
#include "OllamaProvider.hpp"
|
#include "OllamaProvider.hpp"
|
||||||
|
|
||||||
|
#include <LLMCore/ToolsManager.hpp>
|
||||||
|
|
||||||
#include <QJsonArray>
|
#include <QJsonArray>
|
||||||
#include <QJsonDocument>
|
#include <QJsonDocument>
|
||||||
#include <QJsonObject>
|
#include <QJsonObject>
|
||||||
|
|
||||||
#include "llmcore/ValidationUtils.hpp"
|
|
||||||
#include "logger/Logger.hpp"
|
#include "logger/Logger.hpp"
|
||||||
#include "settings/ChatAssistantSettings.hpp"
|
#include "settings/ChatAssistantSettings.hpp"
|
||||||
#include "settings/CodeCompletionSettings.hpp"
|
#include "settings/CodeCompletionSettings.hpp"
|
||||||
#include "settings/QuickRefactorSettings.hpp"
|
#include "settings/QuickRefactorSettings.hpp"
|
||||||
#include "settings/GeneralSettings.hpp"
|
#include "settings/GeneralSettings.hpp"
|
||||||
#include "settings/ProviderSettings.hpp"
|
#include "settings/ProviderSettings.hpp"
|
||||||
|
#include "tools/ToolsRegistration.hpp"
|
||||||
|
|
||||||
namespace QodeAssist::Providers {
|
namespace QodeAssist::Providers {
|
||||||
|
|
||||||
OllamaProvider::OllamaProvider(QObject *parent)
|
OllamaProvider::OllamaProvider(QObject *parent)
|
||||||
: LLMCore::Provider(parent)
|
: PluginLLMCore::Provider(parent)
|
||||||
, m_toolsManager(new Tools::ToolsManager(this))
|
, m_client(new ::LLMCore::OllamaClient(QString(), QString(), QString(), this))
|
||||||
{
|
{
|
||||||
connect(
|
Tools::registerQodeAssistTools(m_client->tools());
|
||||||
m_toolsManager,
|
|
||||||
&Tools::ToolsManager::toolExecutionComplete,
|
|
||||||
this,
|
|
||||||
&OllamaProvider::onToolExecutionComplete);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
QString OllamaProvider::name() const
|
QString OllamaProvider::name() const
|
||||||
@@ -49,6 +47,11 @@ QString OllamaProvider::name() const
|
|||||||
return "Ollama";
|
return "Ollama";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QString OllamaProvider::apiKey() const
|
||||||
|
{
|
||||||
|
return Settings::providerSettings().ollamaBasicAuthApiKey();
|
||||||
|
}
|
||||||
|
|
||||||
QString OllamaProvider::url() const
|
QString OllamaProvider::url() const
|
||||||
{
|
{
|
||||||
return "http://localhost:11434";
|
return "http://localhost:11434";
|
||||||
@@ -64,16 +67,11 @@ QString OllamaProvider::chatEndpoint() const
|
|||||||
return "/api/chat";
|
return "/api/chat";
|
||||||
}
|
}
|
||||||
|
|
||||||
bool OllamaProvider::supportsModelListing() const
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
void OllamaProvider::prepareRequest(
|
void OllamaProvider::prepareRequest(
|
||||||
QJsonObject &request,
|
QJsonObject &request,
|
||||||
LLMCore::PromptTemplate *prompt,
|
PluginLLMCore::PromptTemplate *prompt,
|
||||||
LLMCore::ContextData context,
|
PluginLLMCore::ContextData context,
|
||||||
LLMCore::RequestType type,
|
PluginLLMCore::RequestType type,
|
||||||
bool isToolsEnabled,
|
bool isToolsEnabled,
|
||||||
bool isThinkingEnabled)
|
bool isThinkingEnabled)
|
||||||
{
|
{
|
||||||
@@ -109,9 +107,9 @@ void OllamaProvider::prepareRequest(
|
|||||||
request["options"] = options;
|
request["options"] = options;
|
||||||
};
|
};
|
||||||
|
|
||||||
if (type == LLMCore::RequestType::CodeCompletion) {
|
if (type == PluginLLMCore::RequestType::CodeCompletion) {
|
||||||
applySettings(Settings::codeCompletionSettings());
|
applySettings(Settings::codeCompletionSettings());
|
||||||
} else if (type == LLMCore::RequestType::QuickRefactoring) {
|
} else if (type == PluginLLMCore::RequestType::QuickRefactoring) {
|
||||||
const auto &qrSettings = Settings::quickRefactorSettings();
|
const auto &qrSettings = Settings::quickRefactorSettings();
|
||||||
applySettings(qrSettings);
|
applySettings(qrSettings);
|
||||||
|
|
||||||
@@ -130,13 +128,7 @@ void OllamaProvider::prepareRequest(
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (isToolsEnabled) {
|
if (isToolsEnabled) {
|
||||||
LLMCore::RunToolsFilter filter = LLMCore::RunToolsFilter::ALL;
|
auto toolsDefinitions = m_client->tools()->getToolsDefinitions();
|
||||||
if (type == LLMCore::RequestType::QuickRefactoring) {
|
|
||||||
filter = LLMCore::RunToolsFilter::OnlyRead;
|
|
||||||
}
|
|
||||||
|
|
||||||
auto toolsDefinitions = m_toolsManager->toolsFactory()->getToolsDefinitions(
|
|
||||||
LLMCore::ToolSchemaFormat::Ollama, filter);
|
|
||||||
if (!toolsDefinitions.isEmpty()) {
|
if (!toolsDefinitions.isEmpty()) {
|
||||||
request["tools"] = toolsDefinitions;
|
request["tools"] = toolsDefinitions;
|
||||||
LOG_MESSAGE(
|
LOG_MESSAGE(
|
||||||
@@ -145,453 +137,28 @@ void OllamaProvider::prepareRequest(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
QFuture<QList<QString>> OllamaProvider::getInstalledModels(const QString &url)
|
QFuture<QList<QString>> OllamaProvider::getInstalledModels(const QString &baseUrl)
|
||||||
{
|
{
|
||||||
QNetworkRequest request(QString("%1%2").arg(url, "/api/tags"));
|
m_client->setUrl(baseUrl);
|
||||||
prepareNetworkRequest(request);
|
m_client->setApiKey(Settings::providerSettings().ollamaBasicAuthApiKey());
|
||||||
|
return m_client->listModels();
|
||||||
return httpClient()->get(request).then([](const QByteArray &data) {
|
|
||||||
QList<QString> models;
|
|
||||||
QJsonObject jsonObject = QJsonDocument::fromJson(data).object();
|
|
||||||
QJsonArray modelArray = jsonObject["models"].toArray();
|
|
||||||
|
|
||||||
for (const QJsonValue &value : modelArray) {
|
|
||||||
QJsonObject modelObject = value.toObject();
|
|
||||||
models.append(modelObject["name"].toString());
|
|
||||||
}
|
|
||||||
return models;
|
|
||||||
}).onFailed([](const std::exception &e) {
|
|
||||||
LOG_MESSAGE(QString("Error fetching models: %1").arg(e.what()));
|
|
||||||
return QList<QString>{};
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
QList<QString> OllamaProvider::validateRequest(const QJsonObject &request, LLMCore::TemplateType type)
|
PluginLLMCore::ProviderID OllamaProvider::providerID() const
|
||||||
{
|
{
|
||||||
const auto fimReq = QJsonObject{
|
return PluginLLMCore::ProviderID::Ollama;
|
||||||
{"keep_alive", {}},
|
|
||||||
{"model", {}},
|
|
||||||
{"stream", {}},
|
|
||||||
{"prompt", {}},
|
|
||||||
{"suffix", {}},
|
|
||||||
{"system", {}},
|
|
||||||
{"images", QJsonArray{}},
|
|
||||||
{"options",
|
|
||||||
QJsonObject{
|
|
||||||
{"temperature", {}},
|
|
||||||
{"stop", {}},
|
|
||||||
{"top_p", {}},
|
|
||||||
{"top_k", {}},
|
|
||||||
{"num_predict", {}},
|
|
||||||
{"frequency_penalty", {}},
|
|
||||||
{"presence_penalty", {}}}}};
|
|
||||||
|
|
||||||
const auto messageReq = QJsonObject{
|
|
||||||
{"keep_alive", {}},
|
|
||||||
{"model", {}},
|
|
||||||
{"stream", {}},
|
|
||||||
{"messages", QJsonArray{{QJsonObject{{"role", {}}, {"content", {}}, {"images", QJsonArray{}}}}}},
|
|
||||||
{"tools", QJsonArray{}},
|
|
||||||
{"options",
|
|
||||||
QJsonObject{
|
|
||||||
{"temperature", {}},
|
|
||||||
{"stop", {}},
|
|
||||||
{"top_p", {}},
|
|
||||||
{"top_k", {}},
|
|
||||||
{"num_predict", {}},
|
|
||||||
{"frequency_penalty", {}},
|
|
||||||
{"presence_penalty", {}}}}};
|
|
||||||
|
|
||||||
return LLMCore::ValidationUtils::validateRequestFields(
|
|
||||||
request, type == LLMCore::TemplateType::FIM ? fimReq : messageReq);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
QString OllamaProvider::apiKey() const
|
PluginLLMCore::ProviderCapabilities OllamaProvider::capabilities() const
|
||||||
{
|
{
|
||||||
return {};
|
return PluginLLMCore::ProviderCapability::Tools | PluginLLMCore::ProviderCapability::Thinking
|
||||||
|
| PluginLLMCore::ProviderCapability::Image
|
||||||
|
| PluginLLMCore::ProviderCapability::ModelListing;
|
||||||
}
|
}
|
||||||
|
|
||||||
void OllamaProvider::prepareNetworkRequest(QNetworkRequest &networkRequest) const
|
::LLMCore::BaseClient *OllamaProvider::client() const
|
||||||
{
|
{
|
||||||
networkRequest.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
|
return m_client;
|
||||||
const auto key = Settings::providerSettings().ollamaBasicAuthApiKey();
|
|
||||||
if (!key.isEmpty()) {
|
|
||||||
networkRequest.setRawHeader("Authorization", "Basic " + key.toLatin1());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
LLMCore::ProviderID OllamaProvider::providerID() const
|
|
||||||
{
|
|
||||||
return LLMCore::ProviderID::Ollama;
|
|
||||||
}
|
|
||||||
|
|
||||||
void OllamaProvider::sendRequest(
|
|
||||||
const LLMCore::RequestID &requestId, const QUrl &url, const QJsonObject &payload)
|
|
||||||
{
|
|
||||||
m_dataBuffers[requestId].clear();
|
|
||||||
|
|
||||||
m_requestUrls[requestId] = url;
|
|
||||||
m_originalRequests[requestId] = payload;
|
|
||||||
|
|
||||||
QNetworkRequest networkRequest(url);
|
|
||||||
prepareNetworkRequest(networkRequest);
|
|
||||||
|
|
||||||
LOG_MESSAGE(QString("OllamaProvider: Sending request %1 to %2").arg(requestId, url.toString()));
|
|
||||||
|
|
||||||
httpClient()->postStreaming(requestId, networkRequest, payload);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool OllamaProvider::supportsTools() const
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool OllamaProvider::supportImage() const
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool OllamaProvider::supportThinking() const
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
void OllamaProvider::cancelRequest(const LLMCore::RequestID &requestId)
|
|
||||||
{
|
|
||||||
LOG_MESSAGE(QString("OllamaProvider: Cancelling request %1").arg(requestId));
|
|
||||||
LLMCore::Provider::cancelRequest(requestId);
|
|
||||||
cleanupRequest(requestId);
|
|
||||||
}
|
|
||||||
|
|
||||||
void OllamaProvider::onDataReceived(
|
|
||||||
const QodeAssist::LLMCore::RequestID &requestId, const QByteArray &data)
|
|
||||||
{
|
|
||||||
LLMCore::DataBuffers &buffers = m_dataBuffers[requestId];
|
|
||||||
QStringList lines = buffers.rawStreamBuffer.processData(data);
|
|
||||||
|
|
||||||
if (data.isEmpty()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (const QString &line : lines) {
|
|
||||||
if (line.trimmed().isEmpty()) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
QJsonParseError error;
|
|
||||||
QJsonDocument doc = QJsonDocument::fromJson(line.toUtf8(), &error);
|
|
||||||
if (doc.isNull()) {
|
|
||||||
LOG_MESSAGE(QString("Failed to parse JSON: %1").arg(error.errorString()));
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
QJsonObject obj = doc.object();
|
|
||||||
|
|
||||||
if (obj.contains("error") && !obj["error"].toString().isEmpty()) {
|
|
||||||
LOG_MESSAGE("Error in Ollama response: " + obj["error"].toString());
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
processStreamData(requestId, obj);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void OllamaProvider::onRequestFinished(
|
|
||||||
const QodeAssist::LLMCore::RequestID &requestId, std::optional<QString> error)
|
|
||||||
{
|
|
||||||
if (error) {
|
|
||||||
LOG_MESSAGE(QString("OllamaProvider request %1 failed: %2").arg(requestId, *error));
|
|
||||||
emit requestFailed(requestId, *error);
|
|
||||||
cleanupRequest(requestId);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (m_messages.contains(requestId)) {
|
|
||||||
OllamaMessage *message = m_messages[requestId];
|
|
||||||
if (message->state() == LLMCore::MessageState::RequiresToolExecution) {
|
|
||||||
LOG_MESSAGE(QString("Waiting for tools to complete for %1").arg(requestId));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
QString finalText;
|
|
||||||
if (m_messages.contains(requestId)) {
|
|
||||||
OllamaMessage *message = m_messages[requestId];
|
|
||||||
|
|
||||||
for (auto block : message->currentBlocks()) {
|
|
||||||
if (auto textContent = qobject_cast<LLMCore::TextContent *>(block)) {
|
|
||||||
finalText += textContent->text();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!finalText.isEmpty()) {
|
|
||||||
LOG_MESSAGE(QString("Emitting full response for %1, length=%2")
|
|
||||||
.arg(requestId)
|
|
||||||
.arg(finalText.length()));
|
|
||||||
emit fullResponseReceived(requestId, finalText);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
cleanupRequest(requestId);
|
|
||||||
}
|
|
||||||
|
|
||||||
void OllamaProvider::onToolExecutionComplete(
|
|
||||||
const QString &requestId, const QHash<QString, QString> &toolResults)
|
|
||||||
{
|
|
||||||
if (!m_messages.contains(requestId)) {
|
|
||||||
LOG_MESSAGE(QString("ERROR: No message found for request %1").arg(requestId));
|
|
||||||
cleanupRequest(requestId);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!m_requestUrls.contains(requestId) || !m_originalRequests.contains(requestId)) {
|
|
||||||
LOG_MESSAGE(QString("ERROR: Missing data for continuation request %1").arg(requestId));
|
|
||||||
cleanupRequest(requestId);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
LOG_MESSAGE(QString("Tool execution complete for Ollama request %1").arg(requestId));
|
|
||||||
|
|
||||||
OllamaMessage *message = m_messages[requestId];
|
|
||||||
|
|
||||||
for (auto it = toolResults.begin(); it != toolResults.end(); ++it) {
|
|
||||||
auto toolContent = message->getCurrentToolUseContent();
|
|
||||||
for (auto tool : toolContent) {
|
|
||||||
if (tool->id() == it.key()) {
|
|
||||||
auto toolStringName = m_toolsManager->toolsFactory()->getStringName(tool->name());
|
|
||||||
emit toolExecutionCompleted(requestId, tool->id(), toolStringName, it.value());
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
QJsonObject continuationRequest = m_originalRequests[requestId];
|
|
||||||
QJsonArray messages = continuationRequest["messages"].toArray();
|
|
||||||
|
|
||||||
QJsonObject assistantMessage = message->toProviderFormat();
|
|
||||||
messages.append(assistantMessage);
|
|
||||||
|
|
||||||
LOG_MESSAGE(QString("Assistant message with tool_calls:\n%1")
|
|
||||||
.arg(
|
|
||||||
QString::fromUtf8(
|
|
||||||
QJsonDocument(assistantMessage).toJson(QJsonDocument::Indented))));
|
|
||||||
|
|
||||||
QJsonArray toolResultMessages = message->createToolResultMessages(toolResults);
|
|
||||||
for (const auto &toolMsg : toolResultMessages) {
|
|
||||||
messages.append(toolMsg);
|
|
||||||
LOG_MESSAGE(QString("Tool result message:\n%1")
|
|
||||||
.arg(
|
|
||||||
QString::fromUtf8(
|
|
||||||
QJsonDocument(toolMsg.toObject()).toJson(QJsonDocument::Indented))));
|
|
||||||
}
|
|
||||||
|
|
||||||
continuationRequest["messages"] = messages;
|
|
||||||
|
|
||||||
LOG_MESSAGE(QString("Sending continuation request for %1 with %2 tool results")
|
|
||||||
.arg(requestId)
|
|
||||||
.arg(toolResults.size()));
|
|
||||||
|
|
||||||
sendRequest(requestId, m_requestUrls[requestId], continuationRequest);
|
|
||||||
}
|
|
||||||
|
|
||||||
void OllamaProvider::processStreamData(const QString &requestId, const QJsonObject &data)
|
|
||||||
{
|
|
||||||
OllamaMessage *message = m_messages.value(requestId);
|
|
||||||
if (!message) {
|
|
||||||
message = new OllamaMessage(this);
|
|
||||||
m_messages[requestId] = message;
|
|
||||||
LOG_MESSAGE(QString("Created NEW OllamaMessage for request %1").arg(requestId));
|
|
||||||
|
|
||||||
if (m_dataBuffers.contains(requestId)) {
|
|
||||||
emit continuationStarted(requestId);
|
|
||||||
LOG_MESSAGE(QString("Starting continuation for request %1").arg(requestId));
|
|
||||||
}
|
|
||||||
} else if (
|
|
||||||
m_dataBuffers.contains(requestId)
|
|
||||||
&& message->state() == LLMCore::MessageState::RequiresToolExecution) {
|
|
||||||
message->startNewContinuation();
|
|
||||||
emit continuationStarted(requestId);
|
|
||||||
LOG_MESSAGE(QString("Cleared message state for continuation request %1").arg(requestId));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (data.contains("thinking")) {
|
|
||||||
QString thinkingDelta = data["thinking"].toString();
|
|
||||||
if (!thinkingDelta.isEmpty()) {
|
|
||||||
message->handleThinkingDelta(thinkingDelta);
|
|
||||||
LOG_MESSAGE(QString("OllamaProvider: Received thinking delta, length=%1")
|
|
||||||
.arg(thinkingDelta.length()));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (data.contains("message")) {
|
|
||||||
QJsonObject messageObj = data["message"].toObject();
|
|
||||||
|
|
||||||
if (messageObj.contains("thinking")) {
|
|
||||||
QString thinkingDelta = messageObj["thinking"].toString();
|
|
||||||
if (!thinkingDelta.isEmpty()) {
|
|
||||||
message->handleThinkingDelta(thinkingDelta);
|
|
||||||
|
|
||||||
if (!m_thinkingStarted.contains(requestId)) {
|
|
||||||
auto thinkingBlocks = message->getCurrentThinkingContent();
|
|
||||||
if (!thinkingBlocks.isEmpty() && thinkingBlocks.first()) {
|
|
||||||
QString currentThinking = thinkingBlocks.first()->thinking();
|
|
||||||
QString displayThinking = currentThinking.length() > 50
|
|
||||||
? QString("%1...").arg(currentThinking.left(50))
|
|
||||||
: currentThinking;
|
|
||||||
|
|
||||||
emit thinkingBlockReceived(requestId, displayThinking, "");
|
|
||||||
m_thinkingStarted.insert(requestId);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (messageObj.contains("content")) {
|
|
||||||
QString content = messageObj["content"].toString();
|
|
||||||
if (!content.isEmpty()) {
|
|
||||||
emitThinkingBlocks(requestId, message);
|
|
||||||
|
|
||||||
message->handleContentDelta(content);
|
|
||||||
|
|
||||||
bool hasTextContent = false;
|
|
||||||
for (auto block : message->currentBlocks()) {
|
|
||||||
if (qobject_cast<LLMCore::TextContent *>(block)) {
|
|
||||||
hasTextContent = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (hasTextContent) {
|
|
||||||
LLMCore::DataBuffers &buffers = m_dataBuffers[requestId];
|
|
||||||
buffers.responseContent += content;
|
|
||||||
emit partialResponseReceived(requestId, content);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (messageObj.contains("tool_calls")) {
|
|
||||||
QJsonArray toolCalls = messageObj["tool_calls"].toArray();
|
|
||||||
LOG_MESSAGE(
|
|
||||||
QString("OllamaProvider: Found %1 structured tool calls").arg(toolCalls.size()));
|
|
||||||
for (const auto &toolCallValue : toolCalls) {
|
|
||||||
message->handleToolCall(toolCallValue.toObject());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (data.contains("response")) {
|
|
||||||
QString content = data["response"].toString();
|
|
||||||
if (!content.isEmpty()) {
|
|
||||||
message->handleContentDelta(content);
|
|
||||||
|
|
||||||
bool hasTextContent = false;
|
|
||||||
for (auto block : message->currentBlocks()) {
|
|
||||||
if (qobject_cast<LLMCore::TextContent *>(block)) {
|
|
||||||
hasTextContent = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (hasTextContent) {
|
|
||||||
LLMCore::DataBuffers &buffers = m_dataBuffers[requestId];
|
|
||||||
buffers.responseContent += content;
|
|
||||||
emit partialResponseReceived(requestId, content);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (data["done"].toBool()) {
|
|
||||||
if (data.contains("signature")) {
|
|
||||||
QString signature = data["signature"].toString();
|
|
||||||
message->handleThinkingComplete(signature);
|
|
||||||
LOG_MESSAGE(QString("OllamaProvider: Set thinking signature, length=%1")
|
|
||||||
.arg(signature.length()));
|
|
||||||
}
|
|
||||||
|
|
||||||
message->handleDone(true);
|
|
||||||
handleMessageComplete(requestId);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void OllamaProvider::handleMessageComplete(const QString &requestId)
|
|
||||||
{
|
|
||||||
if (!m_messages.contains(requestId))
|
|
||||||
return;
|
|
||||||
|
|
||||||
OllamaMessage *message = m_messages[requestId];
|
|
||||||
|
|
||||||
emitThinkingBlocks(requestId, message);
|
|
||||||
|
|
||||||
if (message->state() == LLMCore::MessageState::RequiresToolExecution) {
|
|
||||||
LOG_MESSAGE(QString("Ollama message requires tool execution for %1").arg(requestId));
|
|
||||||
|
|
||||||
auto toolUseContent = message->getCurrentToolUseContent();
|
|
||||||
|
|
||||||
if (toolUseContent.isEmpty()) {
|
|
||||||
LOG_MESSAGE(
|
|
||||||
QString("WARNING: No tools to execute for %1 despite RequiresToolExecution state")
|
|
||||||
.arg(requestId));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (auto toolContent : toolUseContent) {
|
|
||||||
auto toolStringName = m_toolsManager->toolsFactory()->getStringName(toolContent->name());
|
|
||||||
emit toolExecutionStarted(requestId, toolContent->id(), toolStringName);
|
|
||||||
|
|
||||||
LOG_MESSAGE(
|
|
||||||
QString("Executing tool: name=%1, id=%2, input=%3")
|
|
||||||
.arg(toolContent->name())
|
|
||||||
.arg(toolContent->id())
|
|
||||||
.arg(
|
|
||||||
QString::fromUtf8(
|
|
||||||
QJsonDocument(toolContent->input()).toJson(QJsonDocument::Compact))));
|
|
||||||
|
|
||||||
m_toolsManager->executeToolCall(
|
|
||||||
requestId, toolContent->id(), toolContent->name(), toolContent->input());
|
|
||||||
}
|
|
||||||
|
|
||||||
} else {
|
|
||||||
LOG_MESSAGE(QString("Ollama message marked as complete for %1").arg(requestId));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void OllamaProvider::cleanupRequest(const LLMCore::RequestID &requestId)
|
|
||||||
{
|
|
||||||
LOG_MESSAGE(QString("Cleaning up Ollama request %1").arg(requestId));
|
|
||||||
|
|
||||||
if (m_messages.contains(requestId)) {
|
|
||||||
auto msg = m_messages.take(requestId);
|
|
||||||
msg->deleteLater();
|
|
||||||
}
|
|
||||||
|
|
||||||
m_dataBuffers.remove(requestId);
|
|
||||||
m_requestUrls.remove(requestId);
|
|
||||||
m_originalRequests.remove(requestId);
|
|
||||||
m_thinkingEmitted.remove(requestId);
|
|
||||||
m_thinkingStarted.remove(requestId);
|
|
||||||
m_toolsManager->cleanupRequest(requestId);
|
|
||||||
}
|
|
||||||
|
|
||||||
void OllamaProvider::emitThinkingBlocks(const QString &requestId, OllamaMessage *message)
|
|
||||||
{
|
|
||||||
if (!message || m_thinkingEmitted.contains(requestId)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
auto thinkingBlocks = message->getCurrentThinkingContent();
|
|
||||||
if (thinkingBlocks.isEmpty()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (auto thinkingContent : thinkingBlocks) {
|
|
||||||
emit thinkingBlockReceived(
|
|
||||||
requestId, thinkingContent->thinking(), thinkingContent->signature());
|
|
||||||
LOG_MESSAGE(QString("Emitted thinking block for request %1, thinking length=%2, signature "
|
|
||||||
"length=%3")
|
|
||||||
.arg(requestId)
|
|
||||||
.arg(thinkingContent->thinking().length())
|
|
||||||
.arg(thinkingContent->signature().length()));
|
|
||||||
}
|
|
||||||
m_thinkingEmitted.insert(requestId);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace QodeAssist::Providers
|
} // namespace QodeAssist::Providers
|
||||||
|
|||||||
@@ -19,14 +19,13 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <llmcore/Provider.hpp>
|
#include <pluginllmcore/Provider.hpp>
|
||||||
|
|
||||||
#include "OllamaMessage.hpp"
|
#include <LLMCore/OllamaClient.hpp>
|
||||||
#include "tools/ToolsManager.hpp"
|
|
||||||
|
|
||||||
namespace QodeAssist::Providers {
|
namespace QodeAssist::Providers {
|
||||||
|
|
||||||
class OllamaProvider : public LLMCore::Provider
|
class OllamaProvider : public PluginLLMCore::Provider
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
public:
|
public:
|
||||||
@@ -36,51 +35,22 @@ public:
|
|||||||
QString url() const override;
|
QString url() const override;
|
||||||
QString completionEndpoint() const override;
|
QString completionEndpoint() const override;
|
||||||
QString chatEndpoint() const override;
|
QString chatEndpoint() const override;
|
||||||
bool supportsModelListing() const override;
|
|
||||||
void prepareRequest(
|
void prepareRequest(
|
||||||
QJsonObject &request,
|
QJsonObject &request,
|
||||||
LLMCore::PromptTemplate *prompt,
|
PluginLLMCore::PromptTemplate *prompt,
|
||||||
LLMCore::ContextData context,
|
PluginLLMCore::ContextData context,
|
||||||
LLMCore::RequestType type,
|
PluginLLMCore::RequestType type,
|
||||||
bool isToolsEnabled,
|
bool isToolsEnabled,
|
||||||
bool isThinkingEnabled) override;
|
bool isThinkingEnabled) override;
|
||||||
QFuture<QList<QString>> getInstalledModels(const QString &url) override;
|
QFuture<QList<QString>> getInstalledModels(const QString &url) override;
|
||||||
QList<QString> validateRequest(const QJsonObject &request, LLMCore::TemplateType type) override;
|
PluginLLMCore::ProviderID providerID() const override;
|
||||||
|
PluginLLMCore::ProviderCapabilities capabilities() const override;
|
||||||
|
|
||||||
|
::LLMCore::BaseClient *client() const override;
|
||||||
QString apiKey() const override;
|
QString apiKey() const override;
|
||||||
void prepareNetworkRequest(QNetworkRequest &networkRequest) const override;
|
|
||||||
LLMCore::ProviderID providerID() const override;
|
|
||||||
|
|
||||||
void sendRequest(
|
|
||||||
const LLMCore::RequestID &requestId, const QUrl &url, const QJsonObject &payload) override;
|
|
||||||
|
|
||||||
bool supportsTools() const override;
|
|
||||||
bool supportImage() const override;
|
|
||||||
bool supportThinking() const override;
|
|
||||||
void cancelRequest(const LLMCore::RequestID &requestId) override;
|
|
||||||
|
|
||||||
public slots:
|
|
||||||
void onDataReceived(
|
|
||||||
const QodeAssist::LLMCore::RequestID &requestId, const QByteArray &data) override;
|
|
||||||
void onRequestFinished(
|
|
||||||
const QodeAssist::LLMCore::RequestID &requestId,
|
|
||||||
std::optional<QString> error) override;
|
|
||||||
|
|
||||||
private slots:
|
|
||||||
void onToolExecutionComplete(
|
|
||||||
const QString &requestId, const QHash<QString, QString> &toolResults);
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void processStreamData(const QString &requestId, const QJsonObject &data);
|
::LLMCore::OllamaClient *m_client;
|
||||||
void handleMessageComplete(const QString &requestId);
|
|
||||||
void cleanupRequest(const LLMCore::RequestID &requestId);
|
|
||||||
void emitThinkingBlocks(const QString &requestId, OllamaMessage *message);
|
|
||||||
|
|
||||||
QHash<QodeAssist::LLMCore::RequestID, OllamaMessage *> m_messages;
|
|
||||||
QHash<QodeAssist::LLMCore::RequestID, QUrl> m_requestUrls;
|
|
||||||
QHash<QodeAssist::LLMCore::RequestID, QJsonObject> m_originalRequests;
|
|
||||||
QSet<QString> m_thinkingEmitted;
|
|
||||||
QSet<QString> m_thinkingStarted;
|
|
||||||
Tools::ToolsManager *m_toolsManager;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace QodeAssist::Providers
|
} // namespace QodeAssist::Providers
|
||||||
|
|||||||
@@ -18,8 +18,9 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "OpenAICompatProvider.hpp"
|
#include "OpenAICompatProvider.hpp"
|
||||||
|
#include <LLMCore/ToolsManager.hpp>
|
||||||
|
|
||||||
#include "llmcore/ValidationUtils.hpp"
|
#include "tools/ToolsRegistration.hpp"
|
||||||
#include "logger/Logger.hpp"
|
#include "logger/Logger.hpp"
|
||||||
#include "settings/ChatAssistantSettings.hpp"
|
#include "settings/ChatAssistantSettings.hpp"
|
||||||
#include "settings/CodeCompletionSettings.hpp"
|
#include "settings/CodeCompletionSettings.hpp"
|
||||||
@@ -30,19 +31,14 @@
|
|||||||
#include <QJsonArray>
|
#include <QJsonArray>
|
||||||
#include <QJsonDocument>
|
#include <QJsonDocument>
|
||||||
#include <QJsonObject>
|
#include <QJsonObject>
|
||||||
#include <QNetworkReply>
|
|
||||||
|
|
||||||
namespace QodeAssist::Providers {
|
namespace QodeAssist::Providers {
|
||||||
|
|
||||||
OpenAICompatProvider::OpenAICompatProvider(QObject *parent)
|
OpenAICompatProvider::OpenAICompatProvider(QObject *parent)
|
||||||
: LLMCore::Provider(parent)
|
: PluginLLMCore::Provider(parent)
|
||||||
, m_toolsManager(new Tools::ToolsManager(this))
|
, m_client(new ::LLMCore::OpenAIClient(QString(), QString(), QString(), this))
|
||||||
{
|
{
|
||||||
connect(
|
Tools::registerQodeAssistTools(m_client->tools());
|
||||||
m_toolsManager,
|
|
||||||
&Tools::ToolsManager::toolExecutionComplete,
|
|
||||||
this,
|
|
||||||
&OpenAICompatProvider::onToolExecutionComplete);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
QString OpenAICompatProvider::name() const
|
QString OpenAICompatProvider::name() const
|
||||||
@@ -50,6 +46,11 @@ QString OpenAICompatProvider::name() const
|
|||||||
return "OpenAI Compatible";
|
return "OpenAI Compatible";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QString OpenAICompatProvider::apiKey() const
|
||||||
|
{
|
||||||
|
return Settings::providerSettings().openAiCompatApiKey();
|
||||||
|
}
|
||||||
|
|
||||||
QString OpenAICompatProvider::url() const
|
QString OpenAICompatProvider::url() const
|
||||||
{
|
{
|
||||||
return "http://localhost:1234";
|
return "http://localhost:1234";
|
||||||
@@ -65,16 +66,11 @@ QString OpenAICompatProvider::chatEndpoint() const
|
|||||||
return "/v1/chat/completions";
|
return "/v1/chat/completions";
|
||||||
}
|
}
|
||||||
|
|
||||||
bool OpenAICompatProvider::supportsModelListing() const
|
|
||||||
{
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
void OpenAICompatProvider::prepareRequest(
|
void OpenAICompatProvider::prepareRequest(
|
||||||
QJsonObject &request,
|
QJsonObject &request,
|
||||||
LLMCore::PromptTemplate *prompt,
|
PluginLLMCore::PromptTemplate *prompt,
|
||||||
LLMCore::ContextData context,
|
PluginLLMCore::ContextData context,
|
||||||
LLMCore::RequestType type,
|
PluginLLMCore::RequestType type,
|
||||||
bool isToolsEnabled,
|
bool isToolsEnabled,
|
||||||
bool isThinkingEnabled)
|
bool isThinkingEnabled)
|
||||||
{
|
{
|
||||||
@@ -98,22 +94,16 @@ void OpenAICompatProvider::prepareRequest(
|
|||||||
request["presence_penalty"] = settings.presencePenalty();
|
request["presence_penalty"] = settings.presencePenalty();
|
||||||
};
|
};
|
||||||
|
|
||||||
if (type == LLMCore::RequestType::CodeCompletion) {
|
if (type == PluginLLMCore::RequestType::CodeCompletion) {
|
||||||
applyModelParams(Settings::codeCompletionSettings());
|
applyModelParams(Settings::codeCompletionSettings());
|
||||||
} else if (type == LLMCore::RequestType::QuickRefactoring) {
|
} else if (type == PluginLLMCore::RequestType::QuickRefactoring) {
|
||||||
applyModelParams(Settings::quickRefactorSettings());
|
applyModelParams(Settings::quickRefactorSettings());
|
||||||
} else {
|
} else {
|
||||||
applyModelParams(Settings::chatAssistantSettings());
|
applyModelParams(Settings::chatAssistantSettings());
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isToolsEnabled) {
|
if (isToolsEnabled) {
|
||||||
LLMCore::RunToolsFilter filter = LLMCore::RunToolsFilter::ALL;
|
auto toolsDefinitions = m_client->tools()->getToolsDefinitions();
|
||||||
if (type == LLMCore::RequestType::QuickRefactoring) {
|
|
||||||
filter = LLMCore::RunToolsFilter::OnlyRead;
|
|
||||||
}
|
|
||||||
|
|
||||||
auto toolsDefinitions = m_toolsManager->getToolsDefinitions(
|
|
||||||
LLMCore::ToolSchemaFormat::OpenAI, filter);
|
|
||||||
if (!toolsDefinitions.isEmpty()) {
|
if (!toolsDefinitions.isEmpty()) {
|
||||||
request["tools"] = toolsDefinitions;
|
request["tools"] = toolsDefinitions;
|
||||||
LOG_MESSAGE(
|
LOG_MESSAGE(
|
||||||
@@ -127,286 +117,19 @@ QFuture<QList<QString>> OpenAICompatProvider::getInstalledModels(const QString &
|
|||||||
return QtFuture::makeReadyFuture(QList<QString>{});
|
return QtFuture::makeReadyFuture(QList<QString>{});
|
||||||
}
|
}
|
||||||
|
|
||||||
QList<QString> OpenAICompatProvider::validateRequest(
|
PluginLLMCore::ProviderID OpenAICompatProvider::providerID() const
|
||||||
const QJsonObject &request, LLMCore::TemplateType type)
|
|
||||||
{
|
{
|
||||||
const auto templateReq = QJsonObject{
|
return PluginLLMCore::ProviderID::OpenAICompatible;
|
||||||
{"model", {}},
|
|
||||||
{"messages", QJsonArray{{QJsonObject{{"role", {}}, {"content", {}}}}}},
|
|
||||||
{"temperature", {}},
|
|
||||||
{"max_tokens", {}},
|
|
||||||
{"top_p", {}},
|
|
||||||
{"top_k", {}},
|
|
||||||
{"frequency_penalty", {}},
|
|
||||||
{"presence_penalty", {}},
|
|
||||||
{"stop", QJsonArray{}},
|
|
||||||
{"stream", {}},
|
|
||||||
{"tools", {}}};
|
|
||||||
|
|
||||||
return LLMCore::ValidationUtils::validateRequestFields(request, templateReq);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
QString OpenAICompatProvider::apiKey() const
|
PluginLLMCore::ProviderCapabilities OpenAICompatProvider::capabilities() const
|
||||||
{
|
{
|
||||||
return Settings::providerSettings().openAiCompatApiKey();
|
return PluginLLMCore::ProviderCapability::Tools | PluginLLMCore::ProviderCapability::Image;
|
||||||
}
|
}
|
||||||
|
|
||||||
void OpenAICompatProvider::prepareNetworkRequest(QNetworkRequest &networkRequest) const
|
::LLMCore::BaseClient *OpenAICompatProvider::client() const
|
||||||
{
|
{
|
||||||
networkRequest.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
|
return m_client;
|
||||||
|
|
||||||
if (!apiKey().isEmpty()) {
|
|
||||||
networkRequest.setRawHeader("Authorization", QString("Bearer %1").arg(apiKey()).toUtf8());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
LLMCore::ProviderID OpenAICompatProvider::providerID() const
|
|
||||||
{
|
|
||||||
return LLMCore::ProviderID::OpenAICompatible;
|
|
||||||
}
|
|
||||||
|
|
||||||
void OpenAICompatProvider::sendRequest(
|
|
||||||
const LLMCore::RequestID &requestId, const QUrl &url, const QJsonObject &payload)
|
|
||||||
{
|
|
||||||
if (!m_messages.contains(requestId)) {
|
|
||||||
m_dataBuffers[requestId].clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
m_requestUrls[requestId] = url;
|
|
||||||
m_originalRequests[requestId] = payload;
|
|
||||||
|
|
||||||
QNetworkRequest networkRequest(url);
|
|
||||||
prepareNetworkRequest(networkRequest);
|
|
||||||
|
|
||||||
LOG_MESSAGE(
|
|
||||||
QString("OpenAICompatProvider: Sending request %1 to %2").arg(requestId, url.toString()));
|
|
||||||
|
|
||||||
httpClient()->postStreaming(requestId, networkRequest, payload);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool OpenAICompatProvider::supportsTools() const
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool OpenAICompatProvider::supportImage() const
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
void OpenAICompatProvider::cancelRequest(const LLMCore::RequestID &requestId)
|
|
||||||
{
|
|
||||||
LOG_MESSAGE(QString("OpenAICompatProvider: Cancelling request %1").arg(requestId));
|
|
||||||
LLMCore::Provider::cancelRequest(requestId);
|
|
||||||
cleanupRequest(requestId);
|
|
||||||
}
|
|
||||||
|
|
||||||
void OpenAICompatProvider::onDataReceived(
|
|
||||||
const QodeAssist::LLMCore::RequestID &requestId, const QByteArray &data)
|
|
||||||
{
|
|
||||||
LLMCore::DataBuffers &buffers = m_dataBuffers[requestId];
|
|
||||||
QStringList lines = buffers.rawStreamBuffer.processData(data);
|
|
||||||
|
|
||||||
for (const QString &line : lines) {
|
|
||||||
if (line.trimmed().isEmpty() || line == "data: [DONE]") {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
QJsonObject chunk = parseEventLine(line);
|
|
||||||
if (chunk.isEmpty())
|
|
||||||
continue;
|
|
||||||
|
|
||||||
processStreamChunk(requestId, chunk);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void OpenAICompatProvider::onRequestFinished(
|
|
||||||
const QodeAssist::LLMCore::RequestID &requestId, std::optional<QString> error)
|
|
||||||
{
|
|
||||||
if (error) {
|
|
||||||
LOG_MESSAGE(QString("OpenAICompatProvider request %1 failed: %2").arg(requestId, *error));
|
|
||||||
emit requestFailed(requestId, *error);
|
|
||||||
cleanupRequest(requestId);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (m_messages.contains(requestId)) {
|
|
||||||
OpenAIMessage *message = m_messages[requestId];
|
|
||||||
if (message->state() == LLMCore::MessageState::RequiresToolExecution) {
|
|
||||||
LOG_MESSAGE(QString("Waiting for tools to complete for %1").arg(requestId));
|
|
||||||
m_dataBuffers.remove(requestId);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (m_dataBuffers.contains(requestId)) {
|
|
||||||
const LLMCore::DataBuffers &buffers = m_dataBuffers[requestId];
|
|
||||||
if (!buffers.responseContent.isEmpty()) {
|
|
||||||
LOG_MESSAGE(QString("Emitting full response for %1").arg(requestId));
|
|
||||||
emit fullResponseReceived(requestId, buffers.responseContent);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
cleanupRequest(requestId);
|
|
||||||
}
|
|
||||||
|
|
||||||
void OpenAICompatProvider::onToolExecutionComplete(
|
|
||||||
const QString &requestId, const QHash<QString, QString> &toolResults)
|
|
||||||
{
|
|
||||||
if (!m_messages.contains(requestId) || !m_requestUrls.contains(requestId)) {
|
|
||||||
LOG_MESSAGE(QString("ERROR: Missing data for continuation request %1").arg(requestId));
|
|
||||||
cleanupRequest(requestId);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
LOG_MESSAGE(QString("Tool execution complete for OpenAICompat request %1").arg(requestId));
|
|
||||||
|
|
||||||
for (auto it = toolResults.begin(); it != toolResults.end(); ++it) {
|
|
||||||
OpenAIMessage *message = m_messages[requestId];
|
|
||||||
auto toolContent = message->getCurrentToolUseContent();
|
|
||||||
for (auto tool : toolContent) {
|
|
||||||
if (tool->id() == it.key()) {
|
|
||||||
auto toolStringName = m_toolsManager->toolsFactory()->getStringName(tool->name());
|
|
||||||
emit toolExecutionCompleted(
|
|
||||||
requestId, tool->id(), toolStringName, toolResults[tool->id()]);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
OpenAIMessage *message = m_messages[requestId];
|
|
||||||
QJsonObject continuationRequest = m_originalRequests[requestId];
|
|
||||||
QJsonArray messages = continuationRequest["messages"].toArray();
|
|
||||||
|
|
||||||
messages.append(message->toProviderFormat());
|
|
||||||
|
|
||||||
QJsonArray toolResultMessages = message->createToolResultMessages(toolResults);
|
|
||||||
for (const auto &toolMsg : toolResultMessages) {
|
|
||||||
messages.append(toolMsg);
|
|
||||||
}
|
|
||||||
|
|
||||||
continuationRequest["messages"] = messages;
|
|
||||||
|
|
||||||
LOG_MESSAGE(QString("Sending continuation request for %1 with %2 tool results")
|
|
||||||
.arg(requestId)
|
|
||||||
.arg(toolResults.size()));
|
|
||||||
|
|
||||||
sendRequest(requestId, m_requestUrls[requestId], continuationRequest);
|
|
||||||
}
|
|
||||||
|
|
||||||
void OpenAICompatProvider::processStreamChunk(const QString &requestId, const QJsonObject &chunk)
|
|
||||||
{
|
|
||||||
QJsonArray choices = chunk["choices"].toArray();
|
|
||||||
if (choices.isEmpty()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
QJsonObject choice = choices[0].toObject();
|
|
||||||
QJsonObject delta = choice["delta"].toObject();
|
|
||||||
QString finishReason = choice["finish_reason"].toString();
|
|
||||||
|
|
||||||
OpenAIMessage *message = m_messages.value(requestId);
|
|
||||||
if (!message) {
|
|
||||||
message = new OpenAIMessage(this);
|
|
||||||
m_messages[requestId] = message;
|
|
||||||
LOG_MESSAGE(QString("Created NEW OpenAIMessage for request %1").arg(requestId));
|
|
||||||
|
|
||||||
if (m_dataBuffers.contains(requestId)) {
|
|
||||||
emit continuationStarted(requestId);
|
|
||||||
LOG_MESSAGE(QString("Starting continuation for request %1").arg(requestId));
|
|
||||||
}
|
|
||||||
} else if (
|
|
||||||
m_dataBuffers.contains(requestId)
|
|
||||||
&& message->state() == LLMCore::MessageState::RequiresToolExecution) {
|
|
||||||
message->startNewContinuation();
|
|
||||||
emit continuationStarted(requestId);
|
|
||||||
LOG_MESSAGE(QString("Cleared message state for continuation request %1").arg(requestId));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (delta.contains("content") && !delta["content"].isNull()) {
|
|
||||||
QString content = delta["content"].toString();
|
|
||||||
message->handleContentDelta(content);
|
|
||||||
|
|
||||||
LLMCore::DataBuffers &buffers = m_dataBuffers[requestId];
|
|
||||||
buffers.responseContent += content;
|
|
||||||
emit partialResponseReceived(requestId, content);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (delta.contains("tool_calls")) {
|
|
||||||
QJsonArray toolCalls = delta["tool_calls"].toArray();
|
|
||||||
for (const auto &toolCallValue : toolCalls) {
|
|
||||||
QJsonObject toolCall = toolCallValue.toObject();
|
|
||||||
int index = toolCall["index"].toInt();
|
|
||||||
|
|
||||||
if (toolCall.contains("id")) {
|
|
||||||
QString id = toolCall["id"].toString();
|
|
||||||
QJsonObject function = toolCall["function"].toObject();
|
|
||||||
QString name = function["name"].toString();
|
|
||||||
message->handleToolCallStart(index, id, name);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (toolCall.contains("function")) {
|
|
||||||
QJsonObject function = toolCall["function"].toObject();
|
|
||||||
if (function.contains("arguments")) {
|
|
||||||
QString args = function["arguments"].toString();
|
|
||||||
message->handleToolCallDelta(index, args);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!finishReason.isEmpty() && finishReason != "null") {
|
|
||||||
for (int i = 0; i < 10; ++i) {
|
|
||||||
message->handleToolCallComplete(i);
|
|
||||||
}
|
|
||||||
|
|
||||||
message->handleFinishReason(finishReason);
|
|
||||||
handleMessageComplete(requestId);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void OpenAICompatProvider::handleMessageComplete(const QString &requestId)
|
|
||||||
{
|
|
||||||
if (!m_messages.contains(requestId))
|
|
||||||
return;
|
|
||||||
|
|
||||||
OpenAIMessage *message = m_messages[requestId];
|
|
||||||
|
|
||||||
if (message->state() == LLMCore::MessageState::RequiresToolExecution) {
|
|
||||||
LOG_MESSAGE(QString("OpenAICompat message requires tool execution for %1").arg(requestId));
|
|
||||||
|
|
||||||
auto toolUseContent = message->getCurrentToolUseContent();
|
|
||||||
|
|
||||||
if (toolUseContent.isEmpty()) {
|
|
||||||
LOG_MESSAGE(QString("No tools to execute for %1").arg(requestId));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (auto toolContent : toolUseContent) {
|
|
||||||
auto toolStringName = m_toolsManager->toolsFactory()->getStringName(toolContent->name());
|
|
||||||
emit toolExecutionStarted(requestId, toolContent->id(), toolStringName);
|
|
||||||
m_toolsManager->executeToolCall(
|
|
||||||
requestId, toolContent->id(), toolContent->name(), toolContent->input());
|
|
||||||
}
|
|
||||||
|
|
||||||
} else {
|
|
||||||
LOG_MESSAGE(QString("OpenAICompat message marked as complete for %1").arg(requestId));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void OpenAICompatProvider::cleanupRequest(const LLMCore::RequestID &requestId)
|
|
||||||
{
|
|
||||||
LOG_MESSAGE(QString("Cleaning up OpenAICompat request %1").arg(requestId));
|
|
||||||
|
|
||||||
if (m_messages.contains(requestId)) {
|
|
||||||
OpenAIMessage *message = m_messages.take(requestId);
|
|
||||||
message->deleteLater();
|
|
||||||
}
|
|
||||||
|
|
||||||
m_dataBuffers.remove(requestId);
|
|
||||||
m_requestUrls.remove(requestId);
|
|
||||||
m_originalRequests.remove(requestId);
|
|
||||||
m_toolsManager->cleanupRequest(requestId);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace QodeAssist::Providers
|
} // namespace QodeAssist::Providers
|
||||||
|
|||||||
@@ -19,13 +19,12 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "OpenAIMessage.hpp"
|
#include <LLMCore/OpenAIClient.hpp>
|
||||||
#include "tools/ToolsManager.hpp"
|
#include <pluginllmcore/Provider.hpp>
|
||||||
#include <llmcore/Provider.hpp>
|
|
||||||
|
|
||||||
namespace QodeAssist::Providers {
|
namespace QodeAssist::Providers {
|
||||||
|
|
||||||
class OpenAICompatProvider : public LLMCore::Provider
|
class OpenAICompatProvider : public PluginLLMCore::Provider
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
public:
|
public:
|
||||||
@@ -35,47 +34,22 @@ public:
|
|||||||
QString url() const override;
|
QString url() const override;
|
||||||
QString completionEndpoint() const override;
|
QString completionEndpoint() const override;
|
||||||
QString chatEndpoint() const override;
|
QString chatEndpoint() const override;
|
||||||
bool supportsModelListing() const override;
|
|
||||||
void prepareRequest(
|
void prepareRequest(
|
||||||
QJsonObject &request,
|
QJsonObject &request,
|
||||||
LLMCore::PromptTemplate *prompt,
|
PluginLLMCore::PromptTemplate *prompt,
|
||||||
LLMCore::ContextData context,
|
PluginLLMCore::ContextData context,
|
||||||
LLMCore::RequestType type,
|
PluginLLMCore::RequestType type,
|
||||||
bool isToolsEnabled,
|
bool isToolsEnabled,
|
||||||
bool isThinkingEnabled) override;
|
bool isThinkingEnabled) override;
|
||||||
QFuture<QList<QString>> getInstalledModels(const QString &url) override;
|
QFuture<QList<QString>> getInstalledModels(const QString &url) override;
|
||||||
QList<QString> validateRequest(const QJsonObject &request, LLMCore::TemplateType type) override;
|
PluginLLMCore::ProviderID providerID() const override;
|
||||||
|
PluginLLMCore::ProviderCapabilities capabilities() const override;
|
||||||
|
|
||||||
|
::LLMCore::BaseClient *client() const override;
|
||||||
QString apiKey() const override;
|
QString apiKey() const override;
|
||||||
void prepareNetworkRequest(QNetworkRequest &networkRequest) const override;
|
|
||||||
LLMCore::ProviderID providerID() const override;
|
|
||||||
|
|
||||||
void sendRequest(
|
|
||||||
const LLMCore::RequestID &requestId, const QUrl &url, const QJsonObject &payload) override;
|
|
||||||
|
|
||||||
bool supportsTools() const override;
|
|
||||||
bool supportImage() const override;
|
|
||||||
void cancelRequest(const LLMCore::RequestID &requestId) override;
|
|
||||||
|
|
||||||
public slots:
|
|
||||||
void onDataReceived(
|
|
||||||
const QodeAssist::LLMCore::RequestID &requestId, const QByteArray &data) override;
|
|
||||||
void onRequestFinished(
|
|
||||||
const QodeAssist::LLMCore::RequestID &requestId,
|
|
||||||
std::optional<QString> error) override;
|
|
||||||
|
|
||||||
private slots:
|
|
||||||
void onToolExecutionComplete(
|
|
||||||
const QString &requestId, const QHash<QString, QString> &toolResults);
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void processStreamChunk(const QString &requestId, const QJsonObject &chunk);
|
::LLMCore::OpenAIClient *m_client;
|
||||||
void handleMessageComplete(const QString &requestId);
|
|
||||||
void cleanupRequest(const LLMCore::RequestID &requestId);
|
|
||||||
|
|
||||||
QHash<LLMCore::RequestID, OpenAIMessage *> m_messages;
|
|
||||||
QHash<LLMCore::RequestID, QUrl> m_requestUrls;
|
|
||||||
QHash<LLMCore::RequestID, QJsonObject> m_originalRequests;
|
|
||||||
Tools::ToolsManager *m_toolsManager;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace QodeAssist::Providers
|
} // namespace QodeAssist::Providers
|
||||||
|
|||||||
@@ -46,7 +46,7 @@ void OpenAIMessage::handleToolCallStart(int index, const QString &id, const QStr
|
|||||||
m_currentBlocks.append(nullptr);
|
m_currentBlocks.append(nullptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
auto toolContent = new LLMCore::ToolUseContent(id, name);
|
auto toolContent = new PluginLLMCore::ToolUseContent(id, name);
|
||||||
toolContent->setParent(this);
|
toolContent->setParent(this);
|
||||||
m_currentBlocks[index] = toolContent;
|
m_currentBlocks[index] = toolContent;
|
||||||
m_pendingToolArguments[index] = "";
|
m_pendingToolArguments[index] = "";
|
||||||
@@ -73,7 +73,7 @@ void OpenAIMessage::handleToolCallComplete(int index)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (index < m_currentBlocks.size()) {
|
if (index < m_currentBlocks.size()) {
|
||||||
if (auto toolContent = qobject_cast<LLMCore::ToolUseContent *>(m_currentBlocks[index])) {
|
if (auto toolContent = qobject_cast<PluginLLMCore::ToolUseContent *>(m_currentBlocks[index])) {
|
||||||
toolContent->setInput(argsObject);
|
toolContent->setInput(argsObject);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -100,10 +100,10 @@ QJsonObject OpenAIMessage::toProviderFormat() const
|
|||||||
if (!block)
|
if (!block)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
if (auto text = qobject_cast<LLMCore::TextContent *>(block)) {
|
if (auto text = qobject_cast<PluginLLMCore::TextContent *>(block)) {
|
||||||
textContent += text->text();
|
textContent += text->text();
|
||||||
} else if (auto tool = qobject_cast<LLMCore::ToolUseContent *>(block)) {
|
} else if (auto tool = qobject_cast<PluginLLMCore::ToolUseContent *>(block)) {
|
||||||
toolCalls.append(tool->toJson(LLMCore::ProviderFormat::OpenAI));
|
toolCalls.append(tool->toJson(PluginLLMCore::ProviderFormat::OpenAI));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -126,20 +126,20 @@ QJsonArray OpenAIMessage::createToolResultMessages(const QHash<QString, QString>
|
|||||||
|
|
||||||
for (auto toolContent : getCurrentToolUseContent()) {
|
for (auto toolContent : getCurrentToolUseContent()) {
|
||||||
if (toolResults.contains(toolContent->id())) {
|
if (toolResults.contains(toolContent->id())) {
|
||||||
auto toolResult = std::make_unique<LLMCore::ToolResultContent>(
|
auto toolResult = std::make_unique<PluginLLMCore::ToolResultContent>(
|
||||||
toolContent->id(), toolResults[toolContent->id()]);
|
toolContent->id(), toolResults[toolContent->id()]);
|
||||||
messages.append(toolResult->toJson(LLMCore::ProviderFormat::OpenAI));
|
messages.append(toolResult->toJson(PluginLLMCore::ProviderFormat::OpenAI));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return messages;
|
return messages;
|
||||||
}
|
}
|
||||||
|
|
||||||
QList<LLMCore::ToolUseContent *> OpenAIMessage::getCurrentToolUseContent() const
|
QList<PluginLLMCore::ToolUseContent *> OpenAIMessage::getCurrentToolUseContent() const
|
||||||
{
|
{
|
||||||
QList<LLMCore::ToolUseContent *> toolBlocks;
|
QList<PluginLLMCore::ToolUseContent *> toolBlocks;
|
||||||
for (auto block : m_currentBlocks) {
|
for (auto block : m_currentBlocks) {
|
||||||
if (auto toolContent = qobject_cast<LLMCore::ToolUseContent *>(block)) {
|
if (auto toolContent = qobject_cast<PluginLLMCore::ToolUseContent *>(block)) {
|
||||||
toolBlocks.append(toolContent);
|
toolBlocks.append(toolContent);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -153,29 +153,29 @@ void OpenAIMessage::startNewContinuation()
|
|||||||
m_currentBlocks.clear();
|
m_currentBlocks.clear();
|
||||||
m_pendingToolArguments.clear();
|
m_pendingToolArguments.clear();
|
||||||
m_finishReason.clear();
|
m_finishReason.clear();
|
||||||
m_state = LLMCore::MessageState::Building;
|
m_state = PluginLLMCore::MessageState::Building;
|
||||||
}
|
}
|
||||||
|
|
||||||
void OpenAIMessage::updateStateFromFinishReason()
|
void OpenAIMessage::updateStateFromFinishReason()
|
||||||
{
|
{
|
||||||
if (m_finishReason == "tool_calls" && !getCurrentToolUseContent().empty()) {
|
if (m_finishReason == "tool_calls" && !getCurrentToolUseContent().empty()) {
|
||||||
m_state = LLMCore::MessageState::RequiresToolExecution;
|
m_state = PluginLLMCore::MessageState::RequiresToolExecution;
|
||||||
} else if (m_finishReason == "stop") {
|
} else if (m_finishReason == "stop") {
|
||||||
m_state = LLMCore::MessageState::Final;
|
m_state = PluginLLMCore::MessageState::Final;
|
||||||
} else {
|
} else {
|
||||||
m_state = LLMCore::MessageState::Complete;
|
m_state = PluginLLMCore::MessageState::Complete;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
LLMCore::TextContent *OpenAIMessage::getOrCreateTextContent()
|
PluginLLMCore::TextContent *OpenAIMessage::getOrCreateTextContent()
|
||||||
{
|
{
|
||||||
for (auto block : m_currentBlocks) {
|
for (auto block : m_currentBlocks) {
|
||||||
if (auto textContent = qobject_cast<LLMCore::TextContent *>(block)) {
|
if (auto textContent = qobject_cast<PluginLLMCore::TextContent *>(block)) {
|
||||||
return textContent;
|
return textContent;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return addCurrentContent<LLMCore::TextContent>();
|
return addCurrentContent<PluginLLMCore::TextContent>();
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace QodeAssist::Providers
|
} // namespace QodeAssist::Providers
|
||||||
|
|||||||
@@ -19,7 +19,7 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <llmcore/ContentBlocks.hpp>
|
#include <pluginllmcore/ContentBlocks.hpp>
|
||||||
|
|
||||||
namespace QodeAssist::Providers {
|
namespace QodeAssist::Providers {
|
||||||
|
|
||||||
@@ -38,19 +38,19 @@ public:
|
|||||||
QJsonObject toProviderFormat() const;
|
QJsonObject toProviderFormat() const;
|
||||||
QJsonArray createToolResultMessages(const QHash<QString, QString> &toolResults) const;
|
QJsonArray createToolResultMessages(const QHash<QString, QString> &toolResults) const;
|
||||||
|
|
||||||
LLMCore::MessageState state() const { return m_state; }
|
PluginLLMCore::MessageState state() const { return m_state; }
|
||||||
QList<LLMCore::ToolUseContent *> getCurrentToolUseContent() const;
|
QList<PluginLLMCore::ToolUseContent *> getCurrentToolUseContent() const;
|
||||||
|
|
||||||
void startNewContinuation();
|
void startNewContinuation();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QString m_finishReason;
|
QString m_finishReason;
|
||||||
LLMCore::MessageState m_state = LLMCore::MessageState::Building;
|
PluginLLMCore::MessageState m_state = PluginLLMCore::MessageState::Building;
|
||||||
QList<LLMCore::ContentBlock *> m_currentBlocks;
|
QList<PluginLLMCore::ContentBlock *> m_currentBlocks;
|
||||||
QHash<int, QString> m_pendingToolArguments;
|
QHash<int, QString> m_pendingToolArguments;
|
||||||
|
|
||||||
void updateStateFromFinishReason();
|
void updateStateFromFinishReason();
|
||||||
LLMCore::TextContent *getOrCreateTextContent();
|
PluginLLMCore::TextContent *getOrCreateTextContent();
|
||||||
|
|
||||||
template<typename T, typename... Args>
|
template<typename T, typename... Args>
|
||||||
T *addCurrentContent(Args &&...args)
|
T *addCurrentContent(Args &&...args)
|
||||||
|
|||||||
@@ -19,7 +19,8 @@
|
|||||||
|
|
||||||
#include "OpenAIProvider.hpp"
|
#include "OpenAIProvider.hpp"
|
||||||
|
|
||||||
#include "llmcore/ValidationUtils.hpp"
|
#include <LLMCore/ToolsManager.hpp>
|
||||||
|
#include "tools/ToolsRegistration.hpp"
|
||||||
#include "logger/Logger.hpp"
|
#include "logger/Logger.hpp"
|
||||||
#include "settings/ChatAssistantSettings.hpp"
|
#include "settings/ChatAssistantSettings.hpp"
|
||||||
#include "settings/CodeCompletionSettings.hpp"
|
#include "settings/CodeCompletionSettings.hpp"
|
||||||
@@ -34,14 +35,10 @@
|
|||||||
namespace QodeAssist::Providers {
|
namespace QodeAssist::Providers {
|
||||||
|
|
||||||
OpenAIProvider::OpenAIProvider(QObject *parent)
|
OpenAIProvider::OpenAIProvider(QObject *parent)
|
||||||
: LLMCore::Provider(parent)
|
: PluginLLMCore::Provider(parent)
|
||||||
, m_toolsManager(new Tools::ToolsManager(this))
|
, m_client(new ::LLMCore::OpenAIClient(QString(), QString(), QString(), this))
|
||||||
{
|
{
|
||||||
connect(
|
Tools::registerQodeAssistTools(m_client->tools());
|
||||||
m_toolsManager,
|
|
||||||
&Tools::ToolsManager::toolExecutionComplete,
|
|
||||||
this,
|
|
||||||
&OpenAIProvider::onToolExecutionComplete);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
QString OpenAIProvider::name() const
|
QString OpenAIProvider::name() const
|
||||||
@@ -49,6 +46,11 @@ QString OpenAIProvider::name() const
|
|||||||
return "OpenAI";
|
return "OpenAI";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QString OpenAIProvider::apiKey() const
|
||||||
|
{
|
||||||
|
return Settings::providerSettings().openAiApiKey();
|
||||||
|
}
|
||||||
|
|
||||||
QString OpenAIProvider::url() const
|
QString OpenAIProvider::url() const
|
||||||
{
|
{
|
||||||
return "https://api.openai.com";
|
return "https://api.openai.com";
|
||||||
@@ -64,16 +66,11 @@ QString OpenAIProvider::chatEndpoint() const
|
|||||||
return "/v1/chat/completions";
|
return "/v1/chat/completions";
|
||||||
}
|
}
|
||||||
|
|
||||||
bool OpenAIProvider::supportsModelListing() const
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
void OpenAIProvider::prepareRequest(
|
void OpenAIProvider::prepareRequest(
|
||||||
QJsonObject &request,
|
QJsonObject &request,
|
||||||
LLMCore::PromptTemplate *prompt,
|
PluginLLMCore::PromptTemplate *prompt,
|
||||||
LLMCore::ContextData context,
|
PluginLLMCore::ContextData context,
|
||||||
LLMCore::RequestType type,
|
PluginLLMCore::RequestType type,
|
||||||
bool isToolsEnabled,
|
bool isToolsEnabled,
|
||||||
bool isThinkingEnabled)
|
bool isThinkingEnabled)
|
||||||
{
|
{
|
||||||
@@ -116,22 +113,16 @@ void OpenAIProvider::prepareRequest(
|
|||||||
request["presence_penalty"] = settings.presencePenalty();
|
request["presence_penalty"] = settings.presencePenalty();
|
||||||
};
|
};
|
||||||
|
|
||||||
if (type == LLMCore::RequestType::CodeCompletion) {
|
if (type == PluginLLMCore::RequestType::CodeCompletion) {
|
||||||
applyModelParams(Settings::codeCompletionSettings());
|
applyModelParams(Settings::codeCompletionSettings());
|
||||||
} else if (type == LLMCore::RequestType::QuickRefactoring) {
|
} else if (type == PluginLLMCore::RequestType::QuickRefactoring) {
|
||||||
applyModelParams(Settings::quickRefactorSettings());
|
applyModelParams(Settings::quickRefactorSettings());
|
||||||
} else {
|
} else {
|
||||||
applyModelParams(Settings::chatAssistantSettings());
|
applyModelParams(Settings::chatAssistantSettings());
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isToolsEnabled) {
|
if (isToolsEnabled) {
|
||||||
LLMCore::RunToolsFilter filter = LLMCore::RunToolsFilter::ALL;
|
auto toolsDefinitions = m_client->tools()->getToolsDefinitions();
|
||||||
if (type == LLMCore::RequestType::QuickRefactoring) {
|
|
||||||
filter = LLMCore::RunToolsFilter::OnlyRead;
|
|
||||||
}
|
|
||||||
|
|
||||||
auto toolsDefinitions = m_toolsManager->getToolsDefinitions(
|
|
||||||
LLMCore::ToolSchemaFormat::OpenAI, filter);
|
|
||||||
if (!toolsDefinitions.isEmpty()) {
|
if (!toolsDefinitions.isEmpty()) {
|
||||||
request["tools"] = toolsDefinitions;
|
request["tools"] = toolsDefinitions;
|
||||||
LOG_MESSAGE(QString("Added %1 tools to OpenAI request").arg(toolsDefinitions.size()));
|
LOG_MESSAGE(QString("Added %1 tools to OpenAI request").arg(toolsDefinitions.size()));
|
||||||
@@ -139,318 +130,37 @@ void OpenAIProvider::prepareRequest(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
QFuture<QList<QString>> OpenAIProvider::getInstalledModels(const QString &url)
|
QFuture<QList<QString>> OpenAIProvider::getInstalledModels(const QString &baseUrl)
|
||||||
{
|
{
|
||||||
QNetworkRequest request(QString("%1/v1/models").arg(url));
|
m_client->setUrl(baseUrl);
|
||||||
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
|
m_client->setApiKey(apiKey());
|
||||||
if (!apiKey().isEmpty()) {
|
return m_client->listModels().then([](const QList<QString> &allModels) {
|
||||||
request.setRawHeader("Authorization", QString("Bearer %1").arg(apiKey()).toUtf8());
|
QList<QString> filtered;
|
||||||
}
|
for (const QString &modelId : allModels) {
|
||||||
|
if (!modelId.contains("dall-e") && !modelId.contains("whisper")
|
||||||
return httpClient()->get(request).then([](const QByteArray &data) {
|
&& !modelId.contains("tts") && !modelId.contains("davinci")
|
||||||
QList<QString> models;
|
&& !modelId.contains("babbage") && !modelId.contains("omni")) {
|
||||||
QJsonObject jsonObject = QJsonDocument::fromJson(data).object();
|
filtered.append(modelId);
|
||||||
|
|
||||||
if (jsonObject.contains("data")) {
|
|
||||||
QJsonArray modelArray = jsonObject["data"].toArray();
|
|
||||||
for (const QJsonValue &value : modelArray) {
|
|
||||||
QJsonObject modelObject = value.toObject();
|
|
||||||
if (modelObject.contains("id")) {
|
|
||||||
QString modelId = modelObject["id"].toString();
|
|
||||||
if (!modelId.contains("dall-e") && !modelId.contains("whisper")
|
|
||||||
&& !modelId.contains("tts") && !modelId.contains("davinci")
|
|
||||||
&& !modelId.contains("babbage") && !modelId.contains("omni")) {
|
|
||||||
models.append(modelId);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return models;
|
return filtered;
|
||||||
}).onFailed([](const std::exception &e) {
|
|
||||||
LOG_MESSAGE(QString("Error fetching OpenAI models: %1").arg(e.what()));
|
|
||||||
return QList<QString>{};
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
QList<QString> OpenAIProvider::validateRequest(const QJsonObject &request, LLMCore::TemplateType type)
|
PluginLLMCore::ProviderID OpenAIProvider::providerID() const
|
||||||
{
|
{
|
||||||
const auto templateReq = QJsonObject{
|
return PluginLLMCore::ProviderID::OpenAI;
|
||||||
{"model", {}},
|
|
||||||
{"messages", QJsonArray{{QJsonObject{{"role", {}}, {"content", {}}}}}},
|
|
||||||
{"temperature", {}},
|
|
||||||
{"max_tokens", {}},
|
|
||||||
{"max_completion_tokens", {}}, // New parameter for newer models
|
|
||||||
{"top_p", {}},
|
|
||||||
{"top_k", {}},
|
|
||||||
{"frequency_penalty", {}},
|
|
||||||
{"presence_penalty", {}},
|
|
||||||
{"stop", QJsonArray{}},
|
|
||||||
{"stream", {}},
|
|
||||||
{"tools", {}}};
|
|
||||||
|
|
||||||
return LLMCore::ValidationUtils::validateRequestFields(request, templateReq);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
QString OpenAIProvider::apiKey() const
|
PluginLLMCore::ProviderCapabilities OpenAIProvider::capabilities() const
|
||||||
{
|
{
|
||||||
return Settings::providerSettings().openAiApiKey();
|
return PluginLLMCore::ProviderCapability::Tools | PluginLLMCore::ProviderCapability::Image
|
||||||
|
| PluginLLMCore::ProviderCapability::ModelListing;
|
||||||
}
|
}
|
||||||
|
|
||||||
void OpenAIProvider::prepareNetworkRequest(QNetworkRequest &networkRequest) const
|
::LLMCore::BaseClient *OpenAIProvider::client() const
|
||||||
{
|
{
|
||||||
networkRequest.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
|
return m_client;
|
||||||
|
|
||||||
if (!apiKey().isEmpty()) {
|
|
||||||
networkRequest.setRawHeader("Authorization", QString("Bearer %1").arg(apiKey()).toUtf8());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
LLMCore::ProviderID OpenAIProvider::providerID() const
|
|
||||||
{
|
|
||||||
return LLMCore::ProviderID::OpenAI;
|
|
||||||
}
|
|
||||||
|
|
||||||
void OpenAIProvider::sendRequest(
|
|
||||||
const LLMCore::RequestID &requestId, const QUrl &url, const QJsonObject &payload)
|
|
||||||
{
|
|
||||||
if (!m_messages.contains(requestId)) {
|
|
||||||
m_dataBuffers[requestId].clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
m_requestUrls[requestId] = url;
|
|
||||||
m_originalRequests[requestId] = payload;
|
|
||||||
|
|
||||||
QNetworkRequest networkRequest(url);
|
|
||||||
prepareNetworkRequest(networkRequest);
|
|
||||||
|
|
||||||
LOG_MESSAGE(QString("OpenAIProvider: Sending request %1 to %2").arg(requestId, url.toString()));
|
|
||||||
|
|
||||||
httpClient()->postStreaming(requestId, networkRequest, payload);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool OpenAIProvider::supportsTools() const
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool OpenAIProvider::supportImage() const
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
void OpenAIProvider::cancelRequest(const LLMCore::RequestID &requestId)
|
|
||||||
{
|
|
||||||
LOG_MESSAGE(QString("OpenAIProvider: Cancelling request %1").arg(requestId));
|
|
||||||
LLMCore::Provider::cancelRequest(requestId);
|
|
||||||
cleanupRequest(requestId);
|
|
||||||
}
|
|
||||||
|
|
||||||
void OpenAIProvider::onDataReceived(
|
|
||||||
const QodeAssist::LLMCore::RequestID &requestId, const QByteArray &data)
|
|
||||||
{
|
|
||||||
LLMCore::DataBuffers &buffers = m_dataBuffers[requestId];
|
|
||||||
QStringList lines = buffers.rawStreamBuffer.processData(data);
|
|
||||||
|
|
||||||
for (const QString &line : lines) {
|
|
||||||
if (line.trimmed().isEmpty() || line == "data: [DONE]") {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
QJsonObject chunk = parseEventLine(line);
|
|
||||||
if (chunk.isEmpty())
|
|
||||||
continue;
|
|
||||||
|
|
||||||
processStreamChunk(requestId, chunk);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void OpenAIProvider::onRequestFinished(
|
|
||||||
const QodeAssist::LLMCore::RequestID &requestId, std::optional<QString> error)
|
|
||||||
{
|
|
||||||
if (error) {
|
|
||||||
LOG_MESSAGE(QString("OpenAIProvider request %1 failed: %2").arg(requestId, *error));
|
|
||||||
emit requestFailed(requestId, *error);
|
|
||||||
cleanupRequest(requestId);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (m_messages.contains(requestId)) {
|
|
||||||
OpenAIMessage *message = m_messages[requestId];
|
|
||||||
if (message->state() == LLMCore::MessageState::RequiresToolExecution) {
|
|
||||||
LOG_MESSAGE(QString("Waiting for tools to complete for %1").arg(requestId));
|
|
||||||
m_dataBuffers.remove(requestId);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (m_dataBuffers.contains(requestId)) {
|
|
||||||
const LLMCore::DataBuffers &buffers = m_dataBuffers[requestId];
|
|
||||||
if (!buffers.responseContent.isEmpty()) {
|
|
||||||
LOG_MESSAGE(QString("Emitting full response for %1").arg(requestId));
|
|
||||||
emit fullResponseReceived(requestId, buffers.responseContent);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
cleanupRequest(requestId);
|
|
||||||
}
|
|
||||||
|
|
||||||
void OpenAIProvider::onToolExecutionComplete(
|
|
||||||
const QString &requestId, const QHash<QString, QString> &toolResults)
|
|
||||||
{
|
|
||||||
if (!m_messages.contains(requestId) || !m_requestUrls.contains(requestId)) {
|
|
||||||
LOG_MESSAGE(QString("ERROR: Missing data for continuation request %1").arg(requestId));
|
|
||||||
cleanupRequest(requestId);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
LOG_MESSAGE(QString("Tool execution complete for OpenAI request %1").arg(requestId));
|
|
||||||
|
|
||||||
for (auto it = toolResults.begin(); it != toolResults.end(); ++it) {
|
|
||||||
OpenAIMessage *message = m_messages[requestId];
|
|
||||||
auto toolContent = message->getCurrentToolUseContent();
|
|
||||||
for (auto tool : toolContent) {
|
|
||||||
if (tool->id() == it.key()) {
|
|
||||||
auto toolStringName = m_toolsManager->toolsFactory()->getStringName(tool->name());
|
|
||||||
emit toolExecutionCompleted(
|
|
||||||
requestId, tool->id(), toolStringName, toolResults[tool->id()]);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
OpenAIMessage *message = m_messages[requestId];
|
|
||||||
QJsonObject continuationRequest = m_originalRequests[requestId];
|
|
||||||
QJsonArray messages = continuationRequest["messages"].toArray();
|
|
||||||
|
|
||||||
messages.append(message->toProviderFormat());
|
|
||||||
|
|
||||||
QJsonArray toolResultMessages = message->createToolResultMessages(toolResults);
|
|
||||||
for (const auto &toolMsg : toolResultMessages) {
|
|
||||||
messages.append(toolMsg);
|
|
||||||
}
|
|
||||||
|
|
||||||
continuationRequest["messages"] = messages;
|
|
||||||
|
|
||||||
LOG_MESSAGE(QString("Sending continuation request for %1 with %2 tool results")
|
|
||||||
.arg(requestId)
|
|
||||||
.arg(toolResults.size()));
|
|
||||||
|
|
||||||
sendRequest(requestId, m_requestUrls[requestId], continuationRequest);
|
|
||||||
}
|
|
||||||
|
|
||||||
void OpenAIProvider::processStreamChunk(const QString &requestId, const QJsonObject &chunk)
|
|
||||||
{
|
|
||||||
QJsonArray choices = chunk["choices"].toArray();
|
|
||||||
if (choices.isEmpty()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
QJsonObject choice = choices[0].toObject();
|
|
||||||
QJsonObject delta = choice["delta"].toObject();
|
|
||||||
QString finishReason = choice["finish_reason"].toString();
|
|
||||||
|
|
||||||
OpenAIMessage *message = m_messages.value(requestId);
|
|
||||||
if (!message) {
|
|
||||||
message = new OpenAIMessage(this);
|
|
||||||
m_messages[requestId] = message;
|
|
||||||
LOG_MESSAGE(QString("Created NEW OpenAIAPIMessage for request %1").arg(requestId));
|
|
||||||
|
|
||||||
if (m_dataBuffers.contains(requestId)) {
|
|
||||||
emit continuationStarted(requestId);
|
|
||||||
LOG_MESSAGE(QString("Starting continuation for request %1").arg(requestId));
|
|
||||||
}
|
|
||||||
} else if (
|
|
||||||
m_dataBuffers.contains(requestId)
|
|
||||||
&& message->state() == LLMCore::MessageState::RequiresToolExecution) {
|
|
||||||
message->startNewContinuation();
|
|
||||||
emit continuationStarted(requestId);
|
|
||||||
LOG_MESSAGE(QString("Cleared message state for continuation request %1").arg(requestId));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (delta.contains("content") && !delta["content"].isNull()) {
|
|
||||||
QString content = delta["content"].toString();
|
|
||||||
message->handleContentDelta(content);
|
|
||||||
|
|
||||||
LLMCore::DataBuffers &buffers = m_dataBuffers[requestId];
|
|
||||||
buffers.responseContent += content;
|
|
||||||
emit partialResponseReceived(requestId, content);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (delta.contains("tool_calls")) {
|
|
||||||
QJsonArray toolCalls = delta["tool_calls"].toArray();
|
|
||||||
for (const auto &toolCallValue : toolCalls) {
|
|
||||||
QJsonObject toolCall = toolCallValue.toObject();
|
|
||||||
int index = toolCall["index"].toInt();
|
|
||||||
|
|
||||||
if (toolCall.contains("id")) {
|
|
||||||
QString id = toolCall["id"].toString();
|
|
||||||
QJsonObject function = toolCall["function"].toObject();
|
|
||||||
QString name = function["name"].toString();
|
|
||||||
message->handleToolCallStart(index, id, name);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (toolCall.contains("function")) {
|
|
||||||
QJsonObject function = toolCall["function"].toObject();
|
|
||||||
if (function.contains("arguments")) {
|
|
||||||
QString args = function["arguments"].toString();
|
|
||||||
message->handleToolCallDelta(index, args);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!finishReason.isEmpty() && finishReason != "null") {
|
|
||||||
for (int i = 0; i < 10; ++i) {
|
|
||||||
message->handleToolCallComplete(i);
|
|
||||||
}
|
|
||||||
|
|
||||||
message->handleFinishReason(finishReason);
|
|
||||||
handleMessageComplete(requestId);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void OpenAIProvider::handleMessageComplete(const QString &requestId)
|
|
||||||
{
|
|
||||||
if (!m_messages.contains(requestId))
|
|
||||||
return;
|
|
||||||
|
|
||||||
OpenAIMessage *message = m_messages[requestId];
|
|
||||||
|
|
||||||
if (message->state() == LLMCore::MessageState::RequiresToolExecution) {
|
|
||||||
LOG_MESSAGE(QString("OpenAI message requires tool execution for %1").arg(requestId));
|
|
||||||
|
|
||||||
auto toolUseContent = message->getCurrentToolUseContent();
|
|
||||||
|
|
||||||
if (toolUseContent.isEmpty()) {
|
|
||||||
LOG_MESSAGE(QString("No tools to execute for %1").arg(requestId));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (auto toolContent : toolUseContent) {
|
|
||||||
auto toolStringName = m_toolsManager->toolsFactory()->getStringName(toolContent->name());
|
|
||||||
emit toolExecutionStarted(requestId, toolContent->id(), toolStringName);
|
|
||||||
m_toolsManager->executeToolCall(
|
|
||||||
requestId, toolContent->id(), toolContent->name(), toolContent->input());
|
|
||||||
}
|
|
||||||
|
|
||||||
} else {
|
|
||||||
LOG_MESSAGE(QString("OpenAI message marked as complete for %1").arg(requestId));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void OpenAIProvider::cleanupRequest(const LLMCore::RequestID &requestId)
|
|
||||||
{
|
|
||||||
LOG_MESSAGE(QString("Cleaning up OpenAI request %1").arg(requestId));
|
|
||||||
|
|
||||||
if (m_messages.contains(requestId)) {
|
|
||||||
OpenAIMessage *message = m_messages.take(requestId);
|
|
||||||
message->deleteLater();
|
|
||||||
}
|
|
||||||
|
|
||||||
m_dataBuffers.remove(requestId);
|
|
||||||
m_requestUrls.remove(requestId);
|
|
||||||
m_originalRequests.remove(requestId);
|
|
||||||
m_toolsManager->cleanupRequest(requestId);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace QodeAssist::Providers
|
} // namespace QodeAssist::Providers
|
||||||
|
|||||||
@@ -19,13 +19,12 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "OpenAIMessage.hpp"
|
#include <LLMCore/OpenAIClient.hpp>
|
||||||
#include "tools/ToolsManager.hpp"
|
#include <pluginllmcore/Provider.hpp>
|
||||||
#include <llmcore/Provider.hpp>
|
|
||||||
|
|
||||||
namespace QodeAssist::Providers {
|
namespace QodeAssist::Providers {
|
||||||
|
|
||||||
class OpenAIProvider : public LLMCore::Provider
|
class OpenAIProvider : public PluginLLMCore::Provider
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
public:
|
public:
|
||||||
@@ -35,47 +34,22 @@ public:
|
|||||||
QString url() const override;
|
QString url() const override;
|
||||||
QString completionEndpoint() const override;
|
QString completionEndpoint() const override;
|
||||||
QString chatEndpoint() const override;
|
QString chatEndpoint() const override;
|
||||||
bool supportsModelListing() const override;
|
|
||||||
void prepareRequest(
|
void prepareRequest(
|
||||||
QJsonObject &request,
|
QJsonObject &request,
|
||||||
LLMCore::PromptTemplate *prompt,
|
PluginLLMCore::PromptTemplate *prompt,
|
||||||
LLMCore::ContextData context,
|
PluginLLMCore::ContextData context,
|
||||||
LLMCore::RequestType type,
|
PluginLLMCore::RequestType type,
|
||||||
bool isToolsEnabled,
|
bool isToolsEnabled,
|
||||||
bool isThinkingEnabled) override;
|
bool isThinkingEnabled) override;
|
||||||
QFuture<QList<QString>> getInstalledModels(const QString &url) override;
|
QFuture<QList<QString>> getInstalledModels(const QString &url) override;
|
||||||
QList<QString> validateRequest(const QJsonObject &request, LLMCore::TemplateType type) override;
|
PluginLLMCore::ProviderID providerID() const override;
|
||||||
|
PluginLLMCore::ProviderCapabilities capabilities() const override;
|
||||||
|
|
||||||
|
::LLMCore::BaseClient *client() const override;
|
||||||
QString apiKey() const override;
|
QString apiKey() const override;
|
||||||
void prepareNetworkRequest(QNetworkRequest &networkRequest) const override;
|
|
||||||
LLMCore::ProviderID providerID() const override;
|
|
||||||
|
|
||||||
void sendRequest(
|
|
||||||
const LLMCore::RequestID &requestId, const QUrl &url, const QJsonObject &payload) override;
|
|
||||||
|
|
||||||
bool supportsTools() const override;
|
|
||||||
bool supportImage() const override;
|
|
||||||
void cancelRequest(const LLMCore::RequestID &requestId) override;
|
|
||||||
|
|
||||||
public slots:
|
|
||||||
void onDataReceived(
|
|
||||||
const QodeAssist::LLMCore::RequestID &requestId, const QByteArray &data) override;
|
|
||||||
void onRequestFinished(
|
|
||||||
const QodeAssist::LLMCore::RequestID &requestId,
|
|
||||||
std::optional<QString> error) override;
|
|
||||||
|
|
||||||
private slots:
|
|
||||||
void onToolExecutionComplete(
|
|
||||||
const QString &requestId, const QHash<QString, QString> &toolResults);
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void processStreamChunk(const QString &requestId, const QJsonObject &chunk);
|
::LLMCore::OpenAIClient *m_client;
|
||||||
void handleMessageComplete(const QString &requestId);
|
|
||||||
void cleanupRequest(const LLMCore::RequestID &requestId);
|
|
||||||
|
|
||||||
QHash<LLMCore::RequestID, OpenAIMessage *> m_messages;
|
|
||||||
QHash<LLMCore::RequestID, QUrl> m_requestUrls;
|
|
||||||
QHash<LLMCore::RequestID, QJsonObject> m_originalRequests;
|
|
||||||
Tools::ToolsManager *m_toolsManager;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace QodeAssist::Providers
|
} // namespace QodeAssist::Providers
|
||||||
|
|||||||
@@ -52,7 +52,7 @@ void OpenAIResponsesMessage::handleItemDelta(const QJsonObject &item)
|
|||||||
|
|
||||||
void OpenAIResponsesMessage::handleToolCallStart(const QString &callId, const QString &name)
|
void OpenAIResponsesMessage::handleToolCallStart(const QString &callId, const QString &name)
|
||||||
{
|
{
|
||||||
auto toolContent = new LLMCore::ToolUseContent(callId, name);
|
auto toolContent = new PluginLLMCore::ToolUseContent(callId, name);
|
||||||
toolContent->setParent(this);
|
toolContent->setParent(this);
|
||||||
m_items.append(toolContent);
|
m_items.append(toolContent);
|
||||||
m_toolCalls[callId] = toolContent;
|
m_toolCalls[callId] = toolContent;
|
||||||
@@ -86,7 +86,7 @@ void OpenAIResponsesMessage::handleToolCallComplete(const QString &callId)
|
|||||||
|
|
||||||
void OpenAIResponsesMessage::handleReasoningStart(const QString &itemId)
|
void OpenAIResponsesMessage::handleReasoningStart(const QString &itemId)
|
||||||
{
|
{
|
||||||
auto thinkingContent = new LLMCore::ThinkingContent();
|
auto thinkingContent = new PluginLLMCore::ThinkingContent();
|
||||||
thinkingContent->setParent(this);
|
thinkingContent->setParent(this);
|
||||||
m_items.append(thinkingContent);
|
m_items.append(thinkingContent);
|
||||||
m_thinkingBlocks[itemId] = thinkingContent;
|
m_thinkingBlocks[itemId] = thinkingContent;
|
||||||
@@ -115,13 +115,13 @@ QList<QJsonObject> OpenAIResponsesMessage::toItemsFormat() const
|
|||||||
QList<QJsonObject> items;
|
QList<QJsonObject> items;
|
||||||
|
|
||||||
QString textContent;
|
QString textContent;
|
||||||
QList<LLMCore::ToolUseContent *> toolCalls;
|
QList<PluginLLMCore::ToolUseContent *> toolCalls;
|
||||||
|
|
||||||
for (const auto *block : m_items) {
|
for (const auto *block : m_items) {
|
||||||
if (const auto *text = qobject_cast<const LLMCore::TextContent *>(block)) {
|
if (const auto *text = qobject_cast<const PluginLLMCore::TextContent *>(block)) {
|
||||||
textContent += text->text();
|
textContent += text->text();
|
||||||
} else if (auto *tool = qobject_cast<LLMCore::ToolUseContent *>(
|
} else if (auto *tool = qobject_cast<PluginLLMCore::ToolUseContent *>(
|
||||||
const_cast<LLMCore::ContentBlock *>(block))) {
|
const_cast<PluginLLMCore::ContentBlock *>(block))) {
|
||||||
toolCalls.append(tool);
|
toolCalls.append(tool);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -146,22 +146,22 @@ QList<QJsonObject> OpenAIResponsesMessage::toItemsFormat() const
|
|||||||
return items;
|
return items;
|
||||||
}
|
}
|
||||||
|
|
||||||
QList<LLMCore::ToolUseContent *> OpenAIResponsesMessage::getCurrentToolUseContent() const
|
QList<PluginLLMCore::ToolUseContent *> OpenAIResponsesMessage::getCurrentToolUseContent() const
|
||||||
{
|
{
|
||||||
QList<LLMCore::ToolUseContent *> toolBlocks;
|
QList<PluginLLMCore::ToolUseContent *> toolBlocks;
|
||||||
for (auto *block : m_items) {
|
for (auto *block : m_items) {
|
||||||
if (auto *toolContent = qobject_cast<LLMCore::ToolUseContent *>(block)) {
|
if (auto *toolContent = qobject_cast<PluginLLMCore::ToolUseContent *>(block)) {
|
||||||
toolBlocks.append(toolContent);
|
toolBlocks.append(toolContent);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return toolBlocks;
|
return toolBlocks;
|
||||||
}
|
}
|
||||||
|
|
||||||
QList<LLMCore::ThinkingContent *> OpenAIResponsesMessage::getCurrentThinkingContent() const
|
QList<PluginLLMCore::ThinkingContent *> OpenAIResponsesMessage::getCurrentThinkingContent() const
|
||||||
{
|
{
|
||||||
QList<LLMCore::ThinkingContent *> thinkingBlocks;
|
QList<PluginLLMCore::ThinkingContent *> thinkingBlocks;
|
||||||
for (auto *block : m_items) {
|
for (auto *block : m_items) {
|
||||||
if (auto *thinkingContent = qobject_cast<LLMCore::ThinkingContent *>(block)) {
|
if (auto *thinkingContent = qobject_cast<PluginLLMCore::ThinkingContent *>(block)) {
|
||||||
thinkingBlocks.append(thinkingContent);
|
thinkingBlocks.append(thinkingContent);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -189,7 +189,7 @@ QString OpenAIResponsesMessage::accumulatedText() const
|
|||||||
{
|
{
|
||||||
QString text;
|
QString text;
|
||||||
for (const auto *block : m_items) {
|
for (const auto *block : m_items) {
|
||||||
if (const auto *textContent = qobject_cast<const LLMCore::TextContent *>(block)) {
|
if (const auto *textContent = qobject_cast<const PluginLLMCore::TextContent *>(block)) {
|
||||||
text += textContent->text();
|
text += textContent->text();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -202,28 +202,28 @@ void OpenAIResponsesMessage::updateStateFromStatus()
|
|||||||
|
|
||||||
if (m_status == "completed") {
|
if (m_status == "completed") {
|
||||||
if (!getCurrentToolUseContent().isEmpty()) {
|
if (!getCurrentToolUseContent().isEmpty()) {
|
||||||
m_state = LLMCore::MessageState::RequiresToolExecution;
|
m_state = PluginLLMCore::MessageState::RequiresToolExecution;
|
||||||
} else {
|
} else {
|
||||||
m_state = LLMCore::MessageState::Complete;
|
m_state = PluginLLMCore::MessageState::Complete;
|
||||||
}
|
}
|
||||||
} else if (m_status == "in_progress") {
|
} else if (m_status == "in_progress") {
|
||||||
m_state = LLMCore::MessageState::Building;
|
m_state = PluginLLMCore::MessageState::Building;
|
||||||
} else if (m_status == "failed" || m_status == "cancelled" || m_status == "incomplete") {
|
} else if (m_status == "failed" || m_status == "cancelled" || m_status == "incomplete") {
|
||||||
m_state = LLMCore::MessageState::Final;
|
m_state = PluginLLMCore::MessageState::Final;
|
||||||
} else {
|
} else {
|
||||||
m_state = LLMCore::MessageState::Building;
|
m_state = PluginLLMCore::MessageState::Building;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
LLMCore::TextContent *OpenAIResponsesMessage::getOrCreateTextItem()
|
PluginLLMCore::TextContent *OpenAIResponsesMessage::getOrCreateTextItem()
|
||||||
{
|
{
|
||||||
for (auto *block : m_items) {
|
for (auto *block : m_items) {
|
||||||
if (auto *textContent = qobject_cast<LLMCore::TextContent *>(block)) {
|
if (auto *textContent = qobject_cast<PluginLLMCore::TextContent *>(block)) {
|
||||||
return textContent;
|
return textContent;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
auto *textContent = new LLMCore::TextContent();
|
auto *textContent = new PluginLLMCore::TextContent();
|
||||||
textContent->setParent(this);
|
textContent->setParent(this);
|
||||||
m_items.append(textContent);
|
m_items.append(textContent);
|
||||||
return textContent;
|
return textContent;
|
||||||
@@ -239,7 +239,7 @@ void OpenAIResponsesMessage::startNewContinuation()
|
|||||||
|
|
||||||
m_pendingToolArguments.clear();
|
m_pendingToolArguments.clear();
|
||||||
m_status.clear();
|
m_status.clear();
|
||||||
m_state = LLMCore::MessageState::Building;
|
m_state = PluginLLMCore::MessageState::Building;
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace QodeAssist::Providers
|
} // namespace QodeAssist::Providers
|
||||||
|
|||||||
@@ -19,7 +19,7 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <llmcore/ContentBlocks.hpp>
|
#include <pluginllmcore/ContentBlocks.hpp>
|
||||||
|
|
||||||
namespace QodeAssist::Providers {
|
namespace QodeAssist::Providers {
|
||||||
|
|
||||||
@@ -41,10 +41,10 @@ public:
|
|||||||
QList<QJsonObject> toItemsFormat() const;
|
QList<QJsonObject> toItemsFormat() const;
|
||||||
QJsonArray createToolResultItems(const QHash<QString, QString> &toolResults) const;
|
QJsonArray createToolResultItems(const QHash<QString, QString> &toolResults) const;
|
||||||
|
|
||||||
LLMCore::MessageState state() const noexcept { return m_state; }
|
PluginLLMCore::MessageState state() const noexcept { return m_state; }
|
||||||
QString accumulatedText() const;
|
QString accumulatedText() const;
|
||||||
QList<LLMCore::ToolUseContent *> getCurrentToolUseContent() const;
|
QList<PluginLLMCore::ToolUseContent *> getCurrentToolUseContent() const;
|
||||||
QList<LLMCore::ThinkingContent *> getCurrentThinkingContent() const;
|
QList<PluginLLMCore::ThinkingContent *> getCurrentThinkingContent() const;
|
||||||
|
|
||||||
bool hasToolCalls() const noexcept { return !m_toolCalls.isEmpty(); }
|
bool hasToolCalls() const noexcept { return !m_toolCalls.isEmpty(); }
|
||||||
bool hasThinkingContent() const noexcept { return !m_thinkingBlocks.isEmpty(); }
|
bool hasThinkingContent() const noexcept { return !m_thinkingBlocks.isEmpty(); }
|
||||||
@@ -53,14 +53,14 @@ public:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
QString m_status;
|
QString m_status;
|
||||||
LLMCore::MessageState m_state = LLMCore::MessageState::Building;
|
PluginLLMCore::MessageState m_state = PluginLLMCore::MessageState::Building;
|
||||||
QList<LLMCore::ContentBlock *> m_items;
|
QList<PluginLLMCore::ContentBlock *> m_items;
|
||||||
QHash<QString, QString> m_pendingToolArguments;
|
QHash<QString, QString> m_pendingToolArguments;
|
||||||
QHash<QString, LLMCore::ToolUseContent *> m_toolCalls;
|
QHash<QString, PluginLLMCore::ToolUseContent *> m_toolCalls;
|
||||||
QHash<QString, LLMCore::ThinkingContent *> m_thinkingBlocks;
|
QHash<QString, PluginLLMCore::ThinkingContent *> m_thinkingBlocks;
|
||||||
|
|
||||||
void updateStateFromStatus();
|
void updateStateFromStatus();
|
||||||
LLMCore::TextContent *getOrCreateTextItem();
|
PluginLLMCore::TextContent *getOrCreateTextItem();
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace QodeAssist::Providers
|
} // namespace QodeAssist::Providers
|
||||||
|
|||||||
@@ -18,9 +18,9 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "OpenAIResponsesProvider.hpp"
|
#include "OpenAIResponsesProvider.hpp"
|
||||||
#include "OpenAIResponses/ResponseObject.hpp"
|
#include <LLMCore/ToolsManager.hpp>
|
||||||
|
#include "tools/ToolsRegistration.hpp"
|
||||||
|
|
||||||
#include "llmcore/ValidationUtils.hpp"
|
|
||||||
#include "logger/Logger.hpp"
|
#include "logger/Logger.hpp"
|
||||||
#include "settings/ChatAssistantSettings.hpp"
|
#include "settings/ChatAssistantSettings.hpp"
|
||||||
#include "settings/CodeCompletionSettings.hpp"
|
#include "settings/CodeCompletionSettings.hpp"
|
||||||
@@ -35,14 +35,10 @@
|
|||||||
namespace QodeAssist::Providers {
|
namespace QodeAssist::Providers {
|
||||||
|
|
||||||
OpenAIResponsesProvider::OpenAIResponsesProvider(QObject *parent)
|
OpenAIResponsesProvider::OpenAIResponsesProvider(QObject *parent)
|
||||||
: LLMCore::Provider(parent)
|
: PluginLLMCore::Provider(parent)
|
||||||
, m_toolsManager(new Tools::ToolsManager(this))
|
, m_client(new ::LLMCore::OpenAIResponsesClient(QString(), QString(), QString(), this))
|
||||||
{
|
{
|
||||||
connect(
|
Tools::registerQodeAssistTools(m_client->tools());
|
||||||
m_toolsManager,
|
|
||||||
&Tools::ToolsManager::toolExecutionComplete,
|
|
||||||
this,
|
|
||||||
&OpenAIResponsesProvider::onToolExecutionComplete);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
QString OpenAIResponsesProvider::name() const
|
QString OpenAIResponsesProvider::name() const
|
||||||
@@ -50,6 +46,11 @@ QString OpenAIResponsesProvider::name() const
|
|||||||
return "OpenAI Responses";
|
return "OpenAI Responses";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QString OpenAIResponsesProvider::apiKey() const
|
||||||
|
{
|
||||||
|
return Settings::providerSettings().openAiApiKey();
|
||||||
|
}
|
||||||
|
|
||||||
QString OpenAIResponsesProvider::url() const
|
QString OpenAIResponsesProvider::url() const
|
||||||
{
|
{
|
||||||
return "https://api.openai.com";
|
return "https://api.openai.com";
|
||||||
@@ -65,16 +66,11 @@ QString OpenAIResponsesProvider::chatEndpoint() const
|
|||||||
return "/v1/responses";
|
return "/v1/responses";
|
||||||
}
|
}
|
||||||
|
|
||||||
bool OpenAIResponsesProvider::supportsModelListing() const
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
void OpenAIResponsesProvider::prepareRequest(
|
void OpenAIResponsesProvider::prepareRequest(
|
||||||
QJsonObject &request,
|
QJsonObject &request,
|
||||||
LLMCore::PromptTemplate *prompt,
|
PluginLLMCore::PromptTemplate *prompt,
|
||||||
LLMCore::ContextData context,
|
PluginLLMCore::ContextData context,
|
||||||
LLMCore::RequestType type,
|
PluginLLMCore::RequestType type,
|
||||||
bool isToolsEnabled,
|
bool isToolsEnabled,
|
||||||
bool isThinkingEnabled)
|
bool isThinkingEnabled)
|
||||||
{
|
{
|
||||||
@@ -109,9 +105,9 @@ void OpenAIResponsesProvider::prepareRequest(
|
|||||||
request["include"] = include;
|
request["include"] = include;
|
||||||
};
|
};
|
||||||
|
|
||||||
if (type == LLMCore::RequestType::CodeCompletion) {
|
if (type == PluginLLMCore::RequestType::CodeCompletion) {
|
||||||
applyModelParams(Settings::codeCompletionSettings());
|
applyModelParams(Settings::codeCompletionSettings());
|
||||||
} else if (type == LLMCore::RequestType::QuickRefactoring) {
|
} else if (type == PluginLLMCore::RequestType::QuickRefactoring) {
|
||||||
const auto &qrSettings = Settings::quickRefactorSettings();
|
const auto &qrSettings = Settings::quickRefactorSettings();
|
||||||
applyModelParams(qrSettings);
|
applyModelParams(qrSettings);
|
||||||
|
|
||||||
@@ -128,12 +124,8 @@ void OpenAIResponsesProvider::prepareRequest(
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (isToolsEnabled) {
|
if (isToolsEnabled) {
|
||||||
const LLMCore::RunToolsFilter filter = (type == LLMCore::RequestType::QuickRefactoring)
|
|
||||||
? LLMCore::RunToolsFilter::OnlyRead
|
|
||||||
: LLMCore::RunToolsFilter::ALL;
|
|
||||||
|
|
||||||
const auto toolsDefinitions
|
const auto toolsDefinitions
|
||||||
= m_toolsManager->getToolsDefinitions(LLMCore::ToolSchemaFormat::OpenAI, filter);
|
= m_client->tools()->getToolsDefinitions();
|
||||||
if (!toolsDefinitions.isEmpty()) {
|
if (!toolsDefinitions.isEmpty()) {
|
||||||
QJsonArray responsesTools;
|
QJsonArray responsesTools;
|
||||||
|
|
||||||
@@ -156,496 +148,40 @@ void OpenAIResponsesProvider::prepareRequest(
|
|||||||
request["stream"] = true;
|
request["stream"] = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
QFuture<QList<QString>> OpenAIResponsesProvider::getInstalledModels(const QString &url)
|
QFuture<QList<QString>> OpenAIResponsesProvider::getInstalledModels(const QString &baseUrl)
|
||||||
{
|
{
|
||||||
QNetworkRequest request(QString("%1/v1/models").arg(url));
|
m_client->setUrl(baseUrl);
|
||||||
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
|
m_client->setApiKey(apiKey());
|
||||||
if (!apiKey().isEmpty()) {
|
return m_client->listModels().then([](const QList<QString> &models) {
|
||||||
request.setRawHeader("Authorization", QString("Bearer %1").arg(apiKey()).toUtf8());
|
QList<QString> filtered;
|
||||||
}
|
static const QStringList modelPrefixes = {"gpt-5", "o1", "o2", "o3", "o4"};
|
||||||
|
for (const QString &modelId : models) {
|
||||||
return httpClient()->get(request).then([](const QByteArray &data) {
|
for (const QString &prefix : modelPrefixes) {
|
||||||
QList<QString> models;
|
if (modelId.contains(prefix)) {
|
||||||
const QJsonObject jsonObject = QJsonDocument::fromJson(data).object();
|
filtered.append(modelId);
|
||||||
|
break;
|
||||||
if (jsonObject.contains("data")) {
|
|
||||||
const QJsonArray modelArray = jsonObject["data"].toArray();
|
|
||||||
models.reserve(modelArray.size());
|
|
||||||
|
|
||||||
static const QStringList modelPrefixes = {"gpt-5", "o1", "o2", "o3", "o4"};
|
|
||||||
|
|
||||||
for (const QJsonValue &value : modelArray) {
|
|
||||||
const QJsonObject modelObject = value.toObject();
|
|
||||||
if (!modelObject.contains("id")) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
const QString modelId = modelObject["id"].toString();
|
|
||||||
for (const QString &prefix : modelPrefixes) {
|
|
||||||
if (modelId.contains(prefix)) {
|
|
||||||
models.append(modelId);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return models;
|
return filtered;
|
||||||
}).onFailed([](const std::exception &e) {
|
|
||||||
LOG_MESSAGE(QString("Error fetching OpenAI models: %1").arg(e.what()));
|
|
||||||
return QList<QString>{};
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
QList<QString> OpenAIResponsesProvider::validateRequest(
|
PluginLLMCore::ProviderID OpenAIResponsesProvider::providerID() const
|
||||||
const QJsonObject &request, LLMCore::TemplateType type)
|
|
||||||
{
|
{
|
||||||
Q_UNUSED(type);
|
return PluginLLMCore::ProviderID::OpenAIResponses;
|
||||||
|
|
||||||
QList<QString> errors;
|
|
||||||
|
|
||||||
if (!request.contains("input")) {
|
|
||||||
errors.append("Missing required field: input");
|
|
||||||
return errors;
|
|
||||||
}
|
|
||||||
|
|
||||||
const QJsonValue inputValue = request["input"];
|
|
||||||
if (!inputValue.isString() && !inputValue.isArray()) {
|
|
||||||
errors.append("Field 'input' must be either a string or an array");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (request.contains("max_output_tokens") && !request["max_output_tokens"].isDouble()) {
|
|
||||||
errors.append("Field 'max_output_tokens' must be a number");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (request.contains("top_p") && !request["top_p"].isDouble()) {
|
|
||||||
errors.append("Field 'top_p' must be a number");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (request.contains("reasoning") && !request["reasoning"].isObject()) {
|
|
||||||
errors.append("Field 'reasoning' must be an object");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (request.contains("stream") && !request["stream"].isBool()) {
|
|
||||||
errors.append("Field 'stream' must be a boolean");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (request.contains("tools") && !request["tools"].isArray()) {
|
|
||||||
errors.append("Field 'tools' must be an array");
|
|
||||||
}
|
|
||||||
|
|
||||||
return errors;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
QString OpenAIResponsesProvider::apiKey() const
|
PluginLLMCore::ProviderCapabilities OpenAIResponsesProvider::capabilities() const
|
||||||
{
|
{
|
||||||
return Settings::providerSettings().openAiApiKey();
|
return PluginLLMCore::ProviderCapability::Tools | PluginLLMCore::ProviderCapability::Thinking
|
||||||
|
| PluginLLMCore::ProviderCapability::Image
|
||||||
|
| PluginLLMCore::ProviderCapability::ModelListing;
|
||||||
}
|
}
|
||||||
|
|
||||||
void OpenAIResponsesProvider::prepareNetworkRequest(QNetworkRequest &networkRequest) const
|
::LLMCore::BaseClient *OpenAIResponsesProvider::client() const
|
||||||
{
|
{
|
||||||
networkRequest.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
|
return m_client;
|
||||||
|
|
||||||
if (!apiKey().isEmpty()) {
|
|
||||||
networkRequest.setRawHeader("Authorization", QString("Bearer %1").arg(apiKey()).toUtf8());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
LLMCore::ProviderID OpenAIResponsesProvider::providerID() const
|
|
||||||
{
|
|
||||||
return LLMCore::ProviderID::OpenAIResponses;
|
|
||||||
}
|
|
||||||
|
|
||||||
void OpenAIResponsesProvider::sendRequest(
|
|
||||||
const LLMCore::RequestID &requestId, const QUrl &url, const QJsonObject &payload)
|
|
||||||
{
|
|
||||||
if (!m_messages.contains(requestId)) {
|
|
||||||
m_dataBuffers[requestId].clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
m_requestUrls[requestId] = url;
|
|
||||||
m_originalRequests[requestId] = payload;
|
|
||||||
|
|
||||||
QNetworkRequest networkRequest(url);
|
|
||||||
prepareNetworkRequest(networkRequest);
|
|
||||||
|
|
||||||
httpClient()->postStreaming(requestId, networkRequest, payload);
|
|
||||||
}
|
|
||||||
|
|
||||||
bool OpenAIResponsesProvider::supportsTools() const
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool OpenAIResponsesProvider::supportImage() const
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool OpenAIResponsesProvider::supportThinking() const
|
|
||||||
{
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
void OpenAIResponsesProvider::cancelRequest(const LLMCore::RequestID &requestId)
|
|
||||||
{
|
|
||||||
LLMCore::Provider::cancelRequest(requestId);
|
|
||||||
cleanupRequest(requestId);
|
|
||||||
}
|
|
||||||
|
|
||||||
void OpenAIResponsesProvider::onDataReceived(
|
|
||||||
const QodeAssist::LLMCore::RequestID &requestId, const QByteArray &data)
|
|
||||||
{
|
|
||||||
LLMCore::DataBuffers &buffers = m_dataBuffers[requestId];
|
|
||||||
const QStringList lines = buffers.rawStreamBuffer.processData(data);
|
|
||||||
|
|
||||||
QString currentEventType;
|
|
||||||
|
|
||||||
for (const QString &line : lines) {
|
|
||||||
const QString trimmedLine = line.trimmed();
|
|
||||||
if (trimmedLine.isEmpty()) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (line == "data: [DONE]") {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (line.startsWith("event: ")) {
|
|
||||||
currentEventType = line.mid(7).trimmed();
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
QString dataLine = line;
|
|
||||||
if (line.startsWith("data: ")) {
|
|
||||||
dataLine = line.mid(6);
|
|
||||||
}
|
|
||||||
|
|
||||||
const QJsonDocument doc = QJsonDocument::fromJson(dataLine.toUtf8());
|
|
||||||
if (doc.isObject()) {
|
|
||||||
const QJsonObject obj = doc.object();
|
|
||||||
processStreamEvent(requestId, currentEventType, obj);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void OpenAIResponsesProvider::onRequestFinished(
|
|
||||||
const QodeAssist::LLMCore::RequestID &requestId, std::optional<QString> error)
|
|
||||||
{
|
|
||||||
if (error) {
|
|
||||||
LOG_MESSAGE(QString("OpenAIResponses request %1 failed: %2").arg(requestId, *error));
|
|
||||||
emit requestFailed(requestId, *error);
|
|
||||||
cleanupRequest(requestId);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (m_messages.contains(requestId)) {
|
|
||||||
OpenAIResponsesMessage *message = m_messages[requestId];
|
|
||||||
if (message->state() == LLMCore::MessageState::RequiresToolExecution) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (m_dataBuffers.contains(requestId)) {
|
|
||||||
const LLMCore::DataBuffers &buffers = m_dataBuffers[requestId];
|
|
||||||
if (!buffers.responseContent.isEmpty()) {
|
|
||||||
emit fullResponseReceived(requestId, buffers.responseContent);
|
|
||||||
} else {
|
|
||||||
LOG_MESSAGE(QString("WARNING: OpenAIResponses - Response content is empty for %1, "
|
|
||||||
"emitting empty response")
|
|
||||||
.arg(requestId));
|
|
||||||
emit fullResponseReceived(requestId, "");
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
LOG_MESSAGE(
|
|
||||||
QString("WARNING: OpenAIResponses - No data buffer found for %1").arg(requestId));
|
|
||||||
}
|
|
||||||
|
|
||||||
cleanupRequest(requestId);
|
|
||||||
}
|
|
||||||
|
|
||||||
void OpenAIResponsesProvider::processStreamEvent(
|
|
||||||
const QString &requestId, const QString &eventType, const QJsonObject &data)
|
|
||||||
{
|
|
||||||
OpenAIResponsesMessage *message = m_messages.value(requestId);
|
|
||||||
if (!message) {
|
|
||||||
message = new OpenAIResponsesMessage(this);
|
|
||||||
m_messages[requestId] = message;
|
|
||||||
|
|
||||||
if (m_dataBuffers.contains(requestId)) {
|
|
||||||
emit continuationStarted(requestId);
|
|
||||||
}
|
|
||||||
} else if (
|
|
||||||
m_dataBuffers.contains(requestId)
|
|
||||||
&& message->state() == LLMCore::MessageState::RequiresToolExecution) {
|
|
||||||
message->startNewContinuation();
|
|
||||||
emit continuationStarted(requestId);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (eventType == "response.content_part.added") {
|
|
||||||
} else if (eventType == "response.output_text.delta") {
|
|
||||||
const QString delta = data["delta"].toString();
|
|
||||||
if (!delta.isEmpty()) {
|
|
||||||
m_dataBuffers[requestId].responseContent += delta;
|
|
||||||
emit partialResponseReceived(requestId, delta);
|
|
||||||
}
|
|
||||||
} else if (eventType == "response.output_text.done") {
|
|
||||||
const QString fullText = data["text"].toString();
|
|
||||||
if (!fullText.isEmpty()) {
|
|
||||||
m_dataBuffers[requestId].responseContent = fullText;
|
|
||||||
}
|
|
||||||
} else if (eventType == "response.content_part.done") {
|
|
||||||
} else if (eventType == "response.output_item.added") {
|
|
||||||
using namespace QodeAssist::OpenAIResponses;
|
|
||||||
const QJsonObject item = data["item"].toObject();
|
|
||||||
OutputItem outputItem = OutputItem::fromJson(item);
|
|
||||||
|
|
||||||
if (const auto *functionCall = outputItem.asFunctionCall()) {
|
|
||||||
if (!functionCall->callId.isEmpty() && !functionCall->name.isEmpty()) {
|
|
||||||
if (!m_itemIdToCallId.contains(requestId)) {
|
|
||||||
m_itemIdToCallId[requestId] = QHash<QString, QString>();
|
|
||||||
}
|
|
||||||
m_itemIdToCallId[requestId][functionCall->id] = functionCall->callId;
|
|
||||||
message->handleToolCallStart(functionCall->callId, functionCall->name);
|
|
||||||
}
|
|
||||||
} else if (const auto *reasoning = outputItem.asReasoning()) {
|
|
||||||
if (!reasoning->id.isEmpty()) {
|
|
||||||
message->handleReasoningStart(reasoning->id);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else if (eventType == "response.reasoning_content.delta") {
|
|
||||||
const QString itemId = data["item_id"].toString();
|
|
||||||
const QString delta = data["delta"].toString();
|
|
||||||
if (!itemId.isEmpty() && !delta.isEmpty()) {
|
|
||||||
message->handleReasoningDelta(itemId, delta);
|
|
||||||
}
|
|
||||||
} else if (eventType == "response.reasoning_content.done") {
|
|
||||||
const QString itemId = data["item_id"].toString();
|
|
||||||
if (!itemId.isEmpty()) {
|
|
||||||
message->handleReasoningComplete(itemId);
|
|
||||||
emitPendingThinkingBlocks(requestId);
|
|
||||||
}
|
|
||||||
} else if (eventType == "response.function_call_arguments.delta") {
|
|
||||||
const QString itemId = data["item_id"].toString();
|
|
||||||
const QString delta = data["delta"].toString();
|
|
||||||
if (!itemId.isEmpty() && !delta.isEmpty()) {
|
|
||||||
const QString callId = m_itemIdToCallId.value(requestId).value(itemId);
|
|
||||||
if (!callId.isEmpty()) {
|
|
||||||
message->handleToolCallDelta(callId, delta);
|
|
||||||
} else {
|
|
||||||
LOG_MESSAGE(QString("ERROR: No call_id mapping found for item_id: %1").arg(itemId));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else if (
|
|
||||||
eventType == "response.function_call_arguments.done"
|
|
||||||
|| eventType == "response.output_item.done") {
|
|
||||||
const QString itemId = data["item_id"].toString();
|
|
||||||
const QJsonObject item = data["item"].toObject();
|
|
||||||
|
|
||||||
if (!item.isEmpty() && item["type"].toString() == "reasoning") {
|
|
||||||
using namespace QodeAssist::OpenAIResponses;
|
|
||||||
|
|
||||||
const QString finalItemId = itemId.isEmpty() ? item["id"].toString() : itemId;
|
|
||||||
|
|
||||||
ReasoningOutput reasoningOutput = ReasoningOutput::fromJson(item);
|
|
||||||
QString reasoningText;
|
|
||||||
|
|
||||||
if (!reasoningOutput.summaryText.isEmpty()) {
|
|
||||||
reasoningText = reasoningOutput.summaryText;
|
|
||||||
} else if (!reasoningOutput.contentTexts.isEmpty()) {
|
|
||||||
reasoningText = reasoningOutput.contentTexts.join("\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (reasoningText.isEmpty()) {
|
|
||||||
reasoningText = QString(
|
|
||||||
"[Reasoning process completed, but detailed thinking is not available in "
|
|
||||||
"streaming mode. The model has processed your request with extended reasoning.]");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!finalItemId.isEmpty()) {
|
|
||||||
message->handleReasoningDelta(finalItemId, reasoningText);
|
|
||||||
message->handleReasoningComplete(finalItemId);
|
|
||||||
emitPendingThinkingBlocks(requestId);
|
|
||||||
}
|
|
||||||
} else if (item.isEmpty() && !itemId.isEmpty()) {
|
|
||||||
const QString callId = m_itemIdToCallId.value(requestId).value(itemId);
|
|
||||||
if (!callId.isEmpty()) {
|
|
||||||
message->handleToolCallComplete(callId);
|
|
||||||
} else {
|
|
||||||
LOG_MESSAGE(
|
|
||||||
QString("ERROR: OpenAIResponses - No call_id mapping found for item_id: %1")
|
|
||||||
.arg(itemId));
|
|
||||||
}
|
|
||||||
} else if (!item.isEmpty() && item["type"].toString() == "function_call") {
|
|
||||||
const QString callId = item["call_id"].toString();
|
|
||||||
if (!callId.isEmpty()) {
|
|
||||||
message->handleToolCallComplete(callId);
|
|
||||||
} else {
|
|
||||||
LOG_MESSAGE(
|
|
||||||
QString("ERROR: OpenAIResponses - Function call done but call_id is empty"));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else if (eventType == "response.created") {
|
|
||||||
} else if (eventType == "response.in_progress") {
|
|
||||||
} else if (eventType == "response.completed") {
|
|
||||||
using namespace QodeAssist::OpenAIResponses;
|
|
||||||
const QJsonObject responseObj = data["response"].toObject();
|
|
||||||
Response response = Response::fromJson(responseObj);
|
|
||||||
|
|
||||||
const QString statusStr = responseObj["status"].toString();
|
|
||||||
|
|
||||||
if (m_dataBuffers[requestId].responseContent.isEmpty()) {
|
|
||||||
const QString aggregatedText = response.getAggregatedText();
|
|
||||||
if (!aggregatedText.isEmpty()) {
|
|
||||||
m_dataBuffers[requestId].responseContent = aggregatedText;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
message->handleStatus(statusStr);
|
|
||||||
handleMessageComplete(requestId);
|
|
||||||
} else if (eventType == "response.incomplete") {
|
|
||||||
using namespace QodeAssist::OpenAIResponses;
|
|
||||||
const QJsonObject responseObj = data["response"].toObject();
|
|
||||||
|
|
||||||
if (!responseObj.isEmpty()) {
|
|
||||||
Response response = Response::fromJson(responseObj);
|
|
||||||
const QString statusStr = responseObj["status"].toString();
|
|
||||||
|
|
||||||
if (m_dataBuffers[requestId].responseContent.isEmpty()) {
|
|
||||||
const QString aggregatedText = response.getAggregatedText();
|
|
||||||
if (!aggregatedText.isEmpty()) {
|
|
||||||
m_dataBuffers[requestId].responseContent = aggregatedText;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
message->handleStatus(statusStr);
|
|
||||||
} else {
|
|
||||||
message->handleStatus("incomplete");
|
|
||||||
}
|
|
||||||
|
|
||||||
handleMessageComplete(requestId);
|
|
||||||
} else if (!eventType.isEmpty()) {
|
|
||||||
LOG_MESSAGE(QString("WARNING: OpenAIResponses - Unhandled event type '%1' for request %2\nData: %3")
|
|
||||||
.arg(eventType)
|
|
||||||
.arg(requestId)
|
|
||||||
.arg(QString::fromUtf8(QJsonDocument(data).toJson(QJsonDocument::Compact))));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void OpenAIResponsesProvider::emitPendingThinkingBlocks(const QString &requestId)
|
|
||||||
{
|
|
||||||
if (!m_messages.contains(requestId)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
OpenAIResponsesMessage *message = m_messages[requestId];
|
|
||||||
const auto thinkingBlocks = message->getCurrentThinkingContent();
|
|
||||||
|
|
||||||
if (thinkingBlocks.isEmpty()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const int alreadyEmitted = m_emittedThinkingBlocksCount.value(requestId, 0);
|
|
||||||
const int totalBlocks = thinkingBlocks.size();
|
|
||||||
|
|
||||||
for (int i = alreadyEmitted; i < totalBlocks; ++i) {
|
|
||||||
const auto *thinkingContent = thinkingBlocks[i];
|
|
||||||
|
|
||||||
if (thinkingContent->thinking().trimmed().isEmpty()) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
emit thinkingBlockReceived(
|
|
||||||
requestId, thinkingContent->thinking(), thinkingContent->signature());
|
|
||||||
}
|
|
||||||
|
|
||||||
m_emittedThinkingBlocksCount[requestId] = totalBlocks;
|
|
||||||
}
|
|
||||||
|
|
||||||
void OpenAIResponsesProvider::handleMessageComplete(const QString &requestId)
|
|
||||||
{
|
|
||||||
if (!m_messages.contains(requestId)) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
OpenAIResponsesMessage *message = m_messages[requestId];
|
|
||||||
|
|
||||||
emitPendingThinkingBlocks(requestId);
|
|
||||||
|
|
||||||
if (message->state() == LLMCore::MessageState::RequiresToolExecution) {
|
|
||||||
const auto toolUseContent = message->getCurrentToolUseContent();
|
|
||||||
|
|
||||||
if (toolUseContent.isEmpty()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (const auto *toolContent : toolUseContent) {
|
|
||||||
const auto toolStringName = m_toolsManager->toolsFactory()->getStringName(
|
|
||||||
toolContent->name());
|
|
||||||
emit toolExecutionStarted(requestId, toolContent->id(), toolStringName);
|
|
||||||
m_toolsManager->executeToolCall(
|
|
||||||
requestId, toolContent->id(), toolContent->name(), toolContent->input());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void OpenAIResponsesProvider::onToolExecutionComplete(
|
|
||||||
const QString &requestId, const QHash<QString, QString> &toolResults)
|
|
||||||
{
|
|
||||||
if (!m_messages.contains(requestId) || !m_requestUrls.contains(requestId)) {
|
|
||||||
LOG_MESSAGE(QString("ERROR: OpenAIResponses - Missing data for continuation request %1")
|
|
||||||
.arg(requestId));
|
|
||||||
cleanupRequest(requestId);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
OpenAIResponsesMessage *message = m_messages[requestId];
|
|
||||||
const auto toolContent = message->getCurrentToolUseContent();
|
|
||||||
|
|
||||||
for (auto it = toolResults.constBegin(); it != toolResults.constEnd(); ++it) {
|
|
||||||
for (const auto *tool : toolContent) {
|
|
||||||
if (tool->id() == it.key()) {
|
|
||||||
const auto toolStringName = m_toolsManager->toolsFactory()->getStringName(
|
|
||||||
tool->name());
|
|
||||||
emit toolExecutionCompleted(
|
|
||||||
requestId, tool->id(), toolStringName, toolResults[tool->id()]);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
QJsonObject continuationRequest = m_originalRequests[requestId];
|
|
||||||
QJsonArray input = continuationRequest["input"].toArray();
|
|
||||||
|
|
||||||
const QList<QJsonObject> assistantItems = message->toItemsFormat();
|
|
||||||
for (const QJsonObject &item : assistantItems) {
|
|
||||||
input.append(item);
|
|
||||||
}
|
|
||||||
|
|
||||||
const QJsonArray toolResultItems = message->createToolResultItems(toolResults);
|
|
||||||
for (const QJsonValue &item : toolResultItems) {
|
|
||||||
input.append(item);
|
|
||||||
}
|
|
||||||
|
|
||||||
continuationRequest["input"] = input;
|
|
||||||
|
|
||||||
m_dataBuffers[requestId].responseContent.clear();
|
|
||||||
|
|
||||||
sendRequest(requestId, m_requestUrls[requestId], continuationRequest);
|
|
||||||
}
|
|
||||||
|
|
||||||
void OpenAIResponsesProvider::cleanupRequest(const LLMCore::RequestID &requestId)
|
|
||||||
{
|
|
||||||
if (m_messages.contains(requestId)) {
|
|
||||||
OpenAIResponsesMessage *message = m_messages.take(requestId);
|
|
||||||
message->deleteLater();
|
|
||||||
}
|
|
||||||
|
|
||||||
m_dataBuffers.remove(requestId);
|
|
||||||
m_requestUrls.remove(requestId);
|
|
||||||
m_originalRequests.remove(requestId);
|
|
||||||
m_itemIdToCallId.remove(requestId);
|
|
||||||
m_emittedThinkingBlocksCount.remove(requestId);
|
|
||||||
m_toolsManager->cleanupRequest(requestId);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace QodeAssist::Providers
|
} // namespace QodeAssist::Providers
|
||||||
|
|||||||
@@ -19,13 +19,12 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "OpenAIResponsesMessage.hpp"
|
#include <LLMCore/OpenAIResponsesClient.hpp>
|
||||||
#include "tools/ToolsManager.hpp"
|
#include <pluginllmcore/Provider.hpp>
|
||||||
#include <llmcore/Provider.hpp>
|
|
||||||
|
|
||||||
namespace QodeAssist::Providers {
|
namespace QodeAssist::Providers {
|
||||||
|
|
||||||
class OpenAIResponsesProvider : public LLMCore::Provider
|
class OpenAIResponsesProvider : public PluginLLMCore::Provider
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
public:
|
public:
|
||||||
@@ -35,52 +34,22 @@ public:
|
|||||||
QString url() const override;
|
QString url() const override;
|
||||||
QString completionEndpoint() const override;
|
QString completionEndpoint() const override;
|
||||||
QString chatEndpoint() const override;
|
QString chatEndpoint() const override;
|
||||||
bool supportsModelListing() const override;
|
|
||||||
void prepareRequest(
|
void prepareRequest(
|
||||||
QJsonObject &request,
|
QJsonObject &request,
|
||||||
LLMCore::PromptTemplate *prompt,
|
PluginLLMCore::PromptTemplate *prompt,
|
||||||
LLMCore::ContextData context,
|
PluginLLMCore::ContextData context,
|
||||||
LLMCore::RequestType type,
|
PluginLLMCore::RequestType type,
|
||||||
bool isToolsEnabled,
|
bool isToolsEnabled,
|
||||||
bool isThinkingEnabled) override;
|
bool isThinkingEnabled) override;
|
||||||
QFuture<QList<QString>> getInstalledModels(const QString &url) override;
|
QFuture<QList<QString>> getInstalledModels(const QString &url) override;
|
||||||
QList<QString> validateRequest(const QJsonObject &request, LLMCore::TemplateType type) override;
|
PluginLLMCore::ProviderID providerID() const override;
|
||||||
|
PluginLLMCore::ProviderCapabilities capabilities() const override;
|
||||||
|
|
||||||
|
::LLMCore::BaseClient *client() const override;
|
||||||
QString apiKey() const override;
|
QString apiKey() const override;
|
||||||
void prepareNetworkRequest(QNetworkRequest &networkRequest) const override;
|
|
||||||
LLMCore::ProviderID providerID() const override;
|
|
||||||
|
|
||||||
void sendRequest(
|
|
||||||
const LLMCore::RequestID &requestId, const QUrl &url, const QJsonObject &payload) override;
|
|
||||||
|
|
||||||
bool supportsTools() const override;
|
|
||||||
bool supportImage() const override;
|
|
||||||
bool supportThinking() const override;
|
|
||||||
void cancelRequest(const LLMCore::RequestID &requestId) override;
|
|
||||||
|
|
||||||
public slots:
|
|
||||||
void onDataReceived(
|
|
||||||
const QodeAssist::LLMCore::RequestID &requestId, const QByteArray &data) override;
|
|
||||||
void onRequestFinished(
|
|
||||||
const QodeAssist::LLMCore::RequestID &requestId,
|
|
||||||
std::optional<QString> error) override;
|
|
||||||
|
|
||||||
private slots:
|
|
||||||
void onToolExecutionComplete(
|
|
||||||
const QString &requestId, const QHash<QString, QString> &toolResults);
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void processStreamEvent(const QString &requestId, const QString &eventType, const QJsonObject &data);
|
::LLMCore::OpenAIResponsesClient *m_client;
|
||||||
void emitPendingThinkingBlocks(const QString &requestId);
|
|
||||||
void handleMessageComplete(const QString &requestId);
|
|
||||||
void cleanupRequest(const LLMCore::RequestID &requestId);
|
|
||||||
|
|
||||||
QHash<LLMCore::RequestID, OpenAIResponsesMessage *> m_messages;
|
|
||||||
QHash<LLMCore::RequestID, QUrl> m_requestUrls;
|
|
||||||
QHash<LLMCore::RequestID, QJsonObject> m_originalRequests;
|
|
||||||
QHash<LLMCore::RequestID, QHash<QString, QString>> m_itemIdToCallId;
|
|
||||||
QHash<LLMCore::RequestID, int> m_emittedThinkingBlocksCount;
|
|
||||||
Tools::ToolsManager *m_toolsManager;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace QodeAssist::Providers
|
} // namespace QodeAssist::Providers
|
||||||
|
|
||||||
|
|||||||
@@ -28,24 +28,28 @@
|
|||||||
|
|
||||||
namespace QodeAssist::Providers {
|
namespace QodeAssist::Providers {
|
||||||
|
|
||||||
|
OpenRouterProvider::OpenRouterProvider(QObject *parent)
|
||||||
|
: OpenAICompatProvider(parent)
|
||||||
|
{}
|
||||||
|
|
||||||
QString OpenRouterProvider::name() const
|
QString OpenRouterProvider::name() const
|
||||||
{
|
{
|
||||||
return "OpenRouter";
|
return "OpenRouter";
|
||||||
}
|
}
|
||||||
|
|
||||||
QString OpenRouterProvider::url() const
|
|
||||||
{
|
|
||||||
return "https://openrouter.ai/api";
|
|
||||||
}
|
|
||||||
|
|
||||||
QString OpenRouterProvider::apiKey() const
|
QString OpenRouterProvider::apiKey() const
|
||||||
{
|
{
|
||||||
return Settings::providerSettings().openRouterApiKey();
|
return Settings::providerSettings().openRouterApiKey();
|
||||||
}
|
}
|
||||||
|
|
||||||
LLMCore::ProviderID OpenRouterProvider::providerID() const
|
QString OpenRouterProvider::url() const
|
||||||
{
|
{
|
||||||
return LLMCore::ProviderID::OpenRouter;
|
return "https://openrouter.ai/api";
|
||||||
|
}
|
||||||
|
|
||||||
|
PluginLLMCore::ProviderID OpenRouterProvider::providerID() const
|
||||||
|
{
|
||||||
|
return PluginLLMCore::ProviderID::OpenRouter;
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace QodeAssist::Providers
|
} // namespace QodeAssist::Providers
|
||||||
|
|||||||
@@ -26,10 +26,12 @@ namespace QodeAssist::Providers {
|
|||||||
class OpenRouterProvider : public OpenAICompatProvider
|
class OpenRouterProvider : public OpenAICompatProvider
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
explicit OpenRouterProvider(QObject *parent = nullptr);
|
||||||
|
|
||||||
QString name() const override;
|
QString name() const override;
|
||||||
QString url() const override;
|
QString url() const override;
|
||||||
QString apiKey() const override;
|
QString apiKey() const override;
|
||||||
LLMCore::ProviderID providerID() const override;
|
PluginLLMCore::ProviderID providerID() const override;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace QodeAssist::Providers
|
} // namespace QodeAssist::Providers
|
||||||
|
|||||||
@@ -19,7 +19,7 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "llmcore/ProvidersManager.hpp"
|
#include "pluginllmcore/ProvidersManager.hpp"
|
||||||
#include "providers/ClaudeProvider.hpp"
|
#include "providers/ClaudeProvider.hpp"
|
||||||
#include "providers/CodestralProvider.hpp"
|
#include "providers/CodestralProvider.hpp"
|
||||||
#include "providers/GoogleAIProvider.hpp"
|
#include "providers/GoogleAIProvider.hpp"
|
||||||
@@ -36,7 +36,7 @@ namespace QodeAssist::Providers {
|
|||||||
|
|
||||||
inline void registerProviders()
|
inline void registerProviders()
|
||||||
{
|
{
|
||||||
auto &providerManager = LLMCore::ProvidersManager::instance();
|
auto &providerManager = PluginLLMCore::ProvidersManager::instance();
|
||||||
providerManager.registerProvider<OllamaProvider>();
|
providerManager.registerProvider<OllamaProvider>();
|
||||||
providerManager.registerProvider<ClaudeProvider>();
|
providerManager.registerProvider<ClaudeProvider>();
|
||||||
providerManager.registerProvider<OpenAIProvider>();
|
providerManager.registerProvider<OpenAIProvider>();
|
||||||
|
|||||||
@@ -50,8 +50,8 @@
|
|||||||
#include "chat/ChatOutputPane.h"
|
#include "chat/ChatOutputPane.h"
|
||||||
#include "chat/NavigationPanel.hpp"
|
#include "chat/NavigationPanel.hpp"
|
||||||
#include "context/DocumentReaderQtCreator.hpp"
|
#include "context/DocumentReaderQtCreator.hpp"
|
||||||
#include "llmcore/PromptProviderFim.hpp"
|
#include "pluginllmcore/PromptProviderFim.hpp"
|
||||||
#include "llmcore/ProvidersManager.hpp"
|
#include "pluginllmcore/ProvidersManager.hpp"
|
||||||
#include "logger/RequestPerformanceLogger.hpp"
|
#include "logger/RequestPerformanceLogger.hpp"
|
||||||
#include "providers/Providers.hpp"
|
#include "providers/Providers.hpp"
|
||||||
#include "settings/ChatAssistantSettings.hpp"
|
#include "settings/ChatAssistantSettings.hpp"
|
||||||
@@ -84,7 +84,7 @@ class QodeAssistPlugin final : public ExtensionSystem::IPlugin
|
|||||||
public:
|
public:
|
||||||
QodeAssistPlugin()
|
QodeAssistPlugin()
|
||||||
: m_updater(new PluginUpdater(this))
|
: m_updater(new PluginUpdater(this))
|
||||||
, m_promptProvider(LLMCore::PromptTemplateManager::instance())
|
, m_promptProvider(PluginLLMCore::PromptTemplateManager::instance())
|
||||||
{}
|
{}
|
||||||
|
|
||||||
~QodeAssistPlugin() final
|
~QodeAssistPlugin() final
|
||||||
@@ -263,7 +263,7 @@ public:
|
|||||||
m_qodeAssistClient = new QodeAssistClient(new LLMClientInterface(
|
m_qodeAssistClient = new QodeAssistClient(new LLMClientInterface(
|
||||||
Settings::generalSettings(),
|
Settings::generalSettings(),
|
||||||
Settings::codeCompletionSettings(),
|
Settings::codeCompletionSettings(),
|
||||||
LLMCore::ProvidersManager::instance(),
|
PluginLLMCore::ProvidersManager::instance(),
|
||||||
&m_promptProvider,
|
&m_promptProvider,
|
||||||
m_documentReader,
|
m_documentReader,
|
||||||
m_performanceLogger));
|
m_performanceLogger));
|
||||||
@@ -305,7 +305,7 @@ private:
|
|||||||
}
|
}
|
||||||
|
|
||||||
QPointer<QodeAssistClient> m_qodeAssistClient;
|
QPointer<QodeAssistClient> m_qodeAssistClient;
|
||||||
LLMCore::PromptProviderFim m_promptProvider;
|
PluginLLMCore::PromptProviderFim m_promptProvider;
|
||||||
Context::DocumentReaderQtCreator m_documentReader;
|
Context::DocumentReaderQtCreator m_documentReader;
|
||||||
RequestPerformanceLogger m_performanceLogger;
|
RequestPerformanceLogger m_performanceLogger;
|
||||||
QPointer<Chat::ChatOutputPane> m_chatOutputPane;
|
QPointer<Chat::ChatOutputPane> m_chatOutputPane;
|
||||||
|
|||||||
@@ -29,7 +29,7 @@ namespace Utils {
|
|||||||
class DetailsWidget;
|
class DetailsWidget;
|
||||||
}
|
}
|
||||||
|
|
||||||
namespace QodeAssist::LLMCore {
|
namespace QodeAssist::PluginLLMCore {
|
||||||
class Provider;
|
class Provider;
|
||||||
}
|
}
|
||||||
namespace QodeAssist::Settings {
|
namespace QodeAssist::Settings {
|
||||||
|
|||||||
1
sources/external/llmcore
vendored
Submodule
1
sources/external/llmcore
vendored
Submodule
Submodule sources/external/llmcore added at d1fb0dca95
@@ -19,21 +19,21 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "llmcore/PromptTemplate.hpp"
|
#include "pluginllmcore/PromptTemplate.hpp"
|
||||||
#include <QJsonArray>
|
#include <QJsonArray>
|
||||||
|
|
||||||
namespace QodeAssist::Templates {
|
namespace QodeAssist::Templates {
|
||||||
|
|
||||||
class Alpaca : public LLMCore::PromptTemplate
|
class Alpaca : public PluginLLMCore::PromptTemplate
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
QString name() const override { return "Alpaca"; }
|
QString name() const override { return "Alpaca"; }
|
||||||
LLMCore::TemplateType type() const override { return LLMCore::TemplateType::Chat; }
|
PluginLLMCore::TemplateType type() const override { return PluginLLMCore::TemplateType::Chat; }
|
||||||
QStringList stopWords() const override
|
QStringList stopWords() const override
|
||||||
{
|
{
|
||||||
return QStringList() << "### Instruction:" << "### Response:";
|
return QStringList() << "### Instruction:" << "### Response:";
|
||||||
}
|
}
|
||||||
void prepareRequest(QJsonObject &request, const LLMCore::ContextData &context) const override
|
void prepareRequest(QJsonObject &request, const PluginLLMCore::ContextData &context) const override
|
||||||
{
|
{
|
||||||
QJsonArray messages;
|
QJsonArray messages;
|
||||||
|
|
||||||
@@ -72,14 +72,14 @@ public:
|
|||||||
"}\n\n"
|
"}\n\n"
|
||||||
"Combines all messages into a single formatted prompt.";
|
"Combines all messages into a single formatted prompt.";
|
||||||
}
|
}
|
||||||
bool isSupportProvider(LLMCore::ProviderID id) const override
|
bool isSupportProvider(PluginLLMCore::ProviderID id) const override
|
||||||
{
|
{
|
||||||
switch (id) {
|
switch (id) {
|
||||||
case LLMCore::ProviderID::Ollama:
|
case PluginLLMCore::ProviderID::Ollama:
|
||||||
case LLMCore::ProviderID::LMStudio:
|
case PluginLLMCore::ProviderID::LMStudio:
|
||||||
case LLMCore::ProviderID::OpenRouter:
|
case PluginLLMCore::ProviderID::OpenRouter:
|
||||||
case LLMCore::ProviderID::OpenAICompatible:
|
case PluginLLMCore::ProviderID::OpenAICompatible:
|
||||||
case LLMCore::ProviderID::LlamaCpp:
|
case PluginLLMCore::ProviderID::LlamaCpp:
|
||||||
return true;
|
return true;
|
||||||
default:
|
default:
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
@@ -21,20 +21,20 @@
|
|||||||
|
|
||||||
#include <QJsonArray>
|
#include <QJsonArray>
|
||||||
|
|
||||||
#include "llmcore/PromptTemplate.hpp"
|
#include "pluginllmcore/PromptTemplate.hpp"
|
||||||
|
|
||||||
namespace QodeAssist::Templates {
|
namespace QodeAssist::Templates {
|
||||||
|
|
||||||
class ChatML : public LLMCore::PromptTemplate
|
class ChatML : public PluginLLMCore::PromptTemplate
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
QString name() const override { return "ChatML"; }
|
QString name() const override { return "ChatML"; }
|
||||||
LLMCore::TemplateType type() const override { return LLMCore::TemplateType::Chat; }
|
PluginLLMCore::TemplateType type() const override { return PluginLLMCore::TemplateType::Chat; }
|
||||||
QStringList stopWords() const override
|
QStringList stopWords() const override
|
||||||
{
|
{
|
||||||
return QStringList() << "<|im_start|>" << "<|im_end|>";
|
return QStringList() << "<|im_start|>" << "<|im_end|>";
|
||||||
}
|
}
|
||||||
void prepareRequest(QJsonObject &request, const LLMCore::ContextData &context) const override
|
void prepareRequest(QJsonObject &request, const PluginLLMCore::ContextData &context) const override
|
||||||
{
|
{
|
||||||
QJsonArray messages;
|
QJsonArray messages;
|
||||||
|
|
||||||
@@ -73,14 +73,14 @@ public:
|
|||||||
"}\n\n"
|
"}\n\n"
|
||||||
"Compatible with multiple providers supporting the ChatML token format.";
|
"Compatible with multiple providers supporting the ChatML token format.";
|
||||||
}
|
}
|
||||||
bool isSupportProvider(LLMCore::ProviderID id) const override
|
bool isSupportProvider(PluginLLMCore::ProviderID id) const override
|
||||||
{
|
{
|
||||||
switch (id) {
|
switch (id) {
|
||||||
case LLMCore::ProviderID::Ollama:
|
case PluginLLMCore::ProviderID::Ollama:
|
||||||
case LLMCore::ProviderID::LMStudio:
|
case PluginLLMCore::ProviderID::LMStudio:
|
||||||
case LLMCore::ProviderID::OpenRouter:
|
case PluginLLMCore::ProviderID::OpenRouter:
|
||||||
case LLMCore::ProviderID::OpenAICompatible:
|
case PluginLLMCore::ProviderID::OpenAICompatible:
|
||||||
case LLMCore::ProviderID::LlamaCpp:
|
case PluginLLMCore::ProviderID::LlamaCpp:
|
||||||
return true;
|
return true;
|
||||||
default:
|
default:
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
@@ -21,17 +21,17 @@
|
|||||||
|
|
||||||
#include <QJsonArray>
|
#include <QJsonArray>
|
||||||
|
|
||||||
#include "llmcore/PromptTemplate.hpp"
|
#include "pluginllmcore/PromptTemplate.hpp"
|
||||||
|
|
||||||
namespace QodeAssist::Templates {
|
namespace QodeAssist::Templates {
|
||||||
|
|
||||||
class Claude : public LLMCore::PromptTemplate
|
class Claude : public PluginLLMCore::PromptTemplate
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
LLMCore::TemplateType type() const override { return LLMCore::TemplateType::Chat; }
|
PluginLLMCore::TemplateType type() const override { return PluginLLMCore::TemplateType::Chat; }
|
||||||
QString name() const override { return "Claude"; }
|
QString name() const override { return "Claude"; }
|
||||||
QStringList stopWords() const override { return QStringList(); }
|
QStringList stopWords() const override { return QStringList(); }
|
||||||
void prepareRequest(QJsonObject &request, const LLMCore::ContextData &context) const override
|
void prepareRequest(QJsonObject &request, const PluginLLMCore::ContextData &context) const override
|
||||||
{
|
{
|
||||||
QJsonArray messages;
|
QJsonArray messages;
|
||||||
|
|
||||||
@@ -111,10 +111,10 @@ public:
|
|||||||
"}\n\n"
|
"}\n\n"
|
||||||
"Formats content according to Claude API specifications.";
|
"Formats content according to Claude API specifications.";
|
||||||
}
|
}
|
||||||
bool isSupportProvider(LLMCore::ProviderID id) const override
|
bool isSupportProvider(PluginLLMCore::ProviderID id) const override
|
||||||
{
|
{
|
||||||
switch (id) {
|
switch (id) {
|
||||||
case QodeAssist::LLMCore::ProviderID::Claude:
|
case QodeAssist::PluginLLMCore::ProviderID::Claude:
|
||||||
return true;
|
return true;
|
||||||
default:
|
default:
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
@@ -19,20 +19,20 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "llmcore/PromptTemplate.hpp"
|
#include "pluginllmcore/PromptTemplate.hpp"
|
||||||
|
|
||||||
namespace QodeAssist::Templates {
|
namespace QodeAssist::Templates {
|
||||||
|
|
||||||
class CodeLlamaFim : public LLMCore::PromptTemplate
|
class CodeLlamaFim : public PluginLLMCore::PromptTemplate
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
LLMCore::TemplateType type() const override { return LLMCore::TemplateType::FIM; }
|
PluginLLMCore::TemplateType type() const override { return PluginLLMCore::TemplateType::FIM; }
|
||||||
QString name() const override { return "CodeLlama FIM"; }
|
QString name() const override { return "CodeLlama FIM"; }
|
||||||
QStringList stopWords() const override
|
QStringList stopWords() const override
|
||||||
{
|
{
|
||||||
return QStringList() << "<EOT>" << "<PRE>" << "<SUF" << "<MID>";
|
return QStringList() << "<EOT>" << "<PRE>" << "<SUF" << "<MID>";
|
||||||
}
|
}
|
||||||
void prepareRequest(QJsonObject &request, const LLMCore::ContextData &context) const override
|
void prepareRequest(QJsonObject &request, const PluginLLMCore::ContextData &context) const override
|
||||||
{
|
{
|
||||||
request["prompt"] = QString("<PRE> %1 <SUF>%2 <MID>")
|
request["prompt"] = QString("<PRE> %1 <SUF>%2 <MID>")
|
||||||
.arg(context.prefix.value_or(""), context.suffix.value_or(""));
|
.arg(context.prefix.value_or(""), context.suffix.value_or(""));
|
||||||
@@ -47,10 +47,10 @@ public:
|
|||||||
"}\n\n"
|
"}\n\n"
|
||||||
"Optimized for code completion with CodeLlama models.";
|
"Optimized for code completion with CodeLlama models.";
|
||||||
}
|
}
|
||||||
bool isSupportProvider(LLMCore::ProviderID id) const override
|
bool isSupportProvider(PluginLLMCore::ProviderID id) const override
|
||||||
{
|
{
|
||||||
switch (id) {
|
switch (id) {
|
||||||
case QodeAssist::LLMCore::ProviderID::Ollama:
|
case QodeAssist::PluginLLMCore::ProviderID::Ollama:
|
||||||
return true;
|
return true;
|
||||||
default:
|
default:
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
@@ -19,21 +19,21 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "llmcore/PromptTemplate.hpp"
|
#include "pluginllmcore/PromptTemplate.hpp"
|
||||||
|
|
||||||
namespace QodeAssist::Templates {
|
namespace QodeAssist::Templates {
|
||||||
|
|
||||||
class CodeLlamaQMLFim : public LLMCore::PromptTemplate
|
class CodeLlamaQMLFim : public PluginLLMCore::PromptTemplate
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
LLMCore::TemplateType type() const override { return LLMCore::TemplateType::FIM; }
|
PluginLLMCore::TemplateType type() const override { return PluginLLMCore::TemplateType::FIM; }
|
||||||
QString name() const override { return "CodeLlama QML FIM"; }
|
QString name() const override { return "CodeLlama QML FIM"; }
|
||||||
QStringList stopWords() const override
|
QStringList stopWords() const override
|
||||||
{
|
{
|
||||||
return QStringList() << "<SUF>" << "<PRE>" << "</PRE>" << "</SUF>" << "< EOT >" << "\\end"
|
return QStringList() << "<SUF>" << "<PRE>" << "</PRE>" << "</SUF>" << "< EOT >" << "\\end"
|
||||||
<< "<MID>" << "</MID>" << "##";
|
<< "<MID>" << "</MID>" << "##";
|
||||||
}
|
}
|
||||||
void prepareRequest(QJsonObject &request, const LLMCore::ContextData &context) const override
|
void prepareRequest(QJsonObject &request, const PluginLLMCore::ContextData &context) const override
|
||||||
{
|
{
|
||||||
request["prompt"] = QString("<SUF>%1<PRE>%2<MID>")
|
request["prompt"] = QString("<SUF>%1<PRE>%2<MID>")
|
||||||
.arg(context.suffix.value_or(""), context.prefix.value_or(""));
|
.arg(context.suffix.value_or(""), context.prefix.value_or(""));
|
||||||
@@ -48,10 +48,10 @@ public:
|
|||||||
"}\n\n"
|
"}\n\n"
|
||||||
"Specifically optimized for QML/JavaScript code completion.";
|
"Specifically optimized for QML/JavaScript code completion.";
|
||||||
}
|
}
|
||||||
bool isSupportProvider(LLMCore::ProviderID id) const override
|
bool isSupportProvider(PluginLLMCore::ProviderID id) const override
|
||||||
{
|
{
|
||||||
switch (id) {
|
switch (id) {
|
||||||
case QodeAssist::LLMCore::ProviderID::Ollama:
|
case QodeAssist::PluginLLMCore::ProviderID::Ollama:
|
||||||
return true;
|
return true;
|
||||||
default:
|
default:
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
@@ -22,18 +22,18 @@
|
|||||||
#include <QJsonArray>
|
#include <QJsonArray>
|
||||||
#include <QJsonObject>
|
#include <QJsonObject>
|
||||||
|
|
||||||
#include "llmcore/PromptTemplate.hpp"
|
#include "pluginllmcore/PromptTemplate.hpp"
|
||||||
|
|
||||||
namespace QodeAssist::Templates {
|
namespace QodeAssist::Templates {
|
||||||
|
|
||||||
class GoogleAI : public LLMCore::PromptTemplate
|
class GoogleAI : public PluginLLMCore::PromptTemplate
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
LLMCore::TemplateType type() const override { return LLMCore::TemplateType::Chat; }
|
PluginLLMCore::TemplateType type() const override { return PluginLLMCore::TemplateType::Chat; }
|
||||||
QString name() const override { return "Google AI"; }
|
QString name() const override { return "Google AI"; }
|
||||||
QStringList stopWords() const override { return QStringList(); }
|
QStringList stopWords() const override { return QStringList(); }
|
||||||
|
|
||||||
void prepareRequest(QJsonObject &request, const LLMCore::ContextData &context) const override
|
void prepareRequest(QJsonObject &request, const PluginLLMCore::ContextData &context) const override
|
||||||
{
|
{
|
||||||
QJsonArray contents;
|
QJsonArray contents;
|
||||||
|
|
||||||
@@ -128,9 +128,9 @@ public:
|
|||||||
"Supports proper role mapping (model/user roles), images, and thinking blocks.";
|
"Supports proper role mapping (model/user roles), images, and thinking blocks.";
|
||||||
}
|
}
|
||||||
|
|
||||||
bool isSupportProvider(LLMCore::ProviderID id) const override
|
bool isSupportProvider(PluginLLMCore::ProviderID id) const override
|
||||||
{
|
{
|
||||||
return id == QodeAssist::LLMCore::ProviderID::GoogleAI;
|
return id == QodeAssist::PluginLLMCore::ProviderID::GoogleAI;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -19,18 +19,18 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "llmcore/PromptTemplate.hpp"
|
#include "pluginllmcore/PromptTemplate.hpp"
|
||||||
#include <QJsonArray>
|
#include <QJsonArray>
|
||||||
|
|
||||||
namespace QodeAssist::Templates {
|
namespace QodeAssist::Templates {
|
||||||
|
|
||||||
class Llama2 : public LLMCore::PromptTemplate
|
class Llama2 : public PluginLLMCore::PromptTemplate
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
QString name() const override { return "Llama 2"; }
|
QString name() const override { return "Llama 2"; }
|
||||||
LLMCore::TemplateType type() const override { return LLMCore::TemplateType::Chat; }
|
PluginLLMCore::TemplateType type() const override { return PluginLLMCore::TemplateType::Chat; }
|
||||||
QStringList stopWords() const override { return QStringList() << "[INST]"; }
|
QStringList stopWords() const override { return QStringList() << "[INST]"; }
|
||||||
void prepareRequest(QJsonObject &request, const LLMCore::ContextData &context) const override
|
void prepareRequest(QJsonObject &request, const PluginLLMCore::ContextData &context) const override
|
||||||
{
|
{
|
||||||
QJsonArray messages;
|
QJsonArray messages;
|
||||||
|
|
||||||
@@ -70,14 +70,14 @@ public:
|
|||||||
"}\n\n"
|
"}\n\n"
|
||||||
"Compatible with Ollama, LM Studio, and other services for Llama 2.";
|
"Compatible with Ollama, LM Studio, and other services for Llama 2.";
|
||||||
}
|
}
|
||||||
bool isSupportProvider(LLMCore::ProviderID id) const override
|
bool isSupportProvider(PluginLLMCore::ProviderID id) const override
|
||||||
{
|
{
|
||||||
switch (id) {
|
switch (id) {
|
||||||
case LLMCore::ProviderID::Ollama:
|
case PluginLLMCore::ProviderID::Ollama:
|
||||||
case LLMCore::ProviderID::LMStudio:
|
case PluginLLMCore::ProviderID::LMStudio:
|
||||||
case LLMCore::ProviderID::OpenRouter:
|
case PluginLLMCore::ProviderID::OpenRouter:
|
||||||
case LLMCore::ProviderID::OpenAICompatible:
|
case PluginLLMCore::ProviderID::OpenAICompatible:
|
||||||
case LLMCore::ProviderID::LlamaCpp:
|
case PluginLLMCore::ProviderID::LlamaCpp:
|
||||||
return true;
|
return true;
|
||||||
default:
|
default:
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
@@ -21,20 +21,20 @@
|
|||||||
|
|
||||||
#include <QJsonArray>
|
#include <QJsonArray>
|
||||||
|
|
||||||
#include "llmcore/PromptTemplate.hpp"
|
#include "pluginllmcore/PromptTemplate.hpp"
|
||||||
|
|
||||||
namespace QodeAssist::Templates {
|
namespace QodeAssist::Templates {
|
||||||
|
|
||||||
class Llama3 : public LLMCore::PromptTemplate
|
class Llama3 : public PluginLLMCore::PromptTemplate
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
QString name() const override { return "Llama 3"; }
|
QString name() const override { return "Llama 3"; }
|
||||||
LLMCore::TemplateType type() const override { return LLMCore::TemplateType::Chat; }
|
PluginLLMCore::TemplateType type() const override { return PluginLLMCore::TemplateType::Chat; }
|
||||||
QStringList stopWords() const override
|
QStringList stopWords() const override
|
||||||
{
|
{
|
||||||
return QStringList() << "<|start_header_id|>" << "<|end_header_id|>" << "<|eot_id|>";
|
return QStringList() << "<|start_header_id|>" << "<|end_header_id|>" << "<|eot_id|>";
|
||||||
}
|
}
|
||||||
void prepareRequest(QJsonObject &request, const LLMCore::ContextData &context) const override
|
void prepareRequest(QJsonObject &request, const PluginLLMCore::ContextData &context) const override
|
||||||
{
|
{
|
||||||
QJsonArray messages;
|
QJsonArray messages;
|
||||||
|
|
||||||
@@ -77,14 +77,14 @@ public:
|
|||||||
"}\n\n"
|
"}\n\n"
|
||||||
"Compatible with Ollama, LM Studio, and OpenAI-compatible services for Llama 3.";
|
"Compatible with Ollama, LM Studio, and OpenAI-compatible services for Llama 3.";
|
||||||
}
|
}
|
||||||
bool isSupportProvider(LLMCore::ProviderID id) const override
|
bool isSupportProvider(PluginLLMCore::ProviderID id) const override
|
||||||
{
|
{
|
||||||
switch (id) {
|
switch (id) {
|
||||||
case LLMCore::ProviderID::Ollama:
|
case PluginLLMCore::ProviderID::Ollama:
|
||||||
case LLMCore::ProviderID::LMStudio:
|
case PluginLLMCore::ProviderID::LMStudio:
|
||||||
case LLMCore::ProviderID::OpenRouter:
|
case PluginLLMCore::ProviderID::OpenRouter:
|
||||||
case LLMCore::ProviderID::OpenAICompatible:
|
case PluginLLMCore::ProviderID::OpenAICompatible:
|
||||||
case LLMCore::ProviderID::LlamaCpp:
|
case PluginLLMCore::ProviderID::LlamaCpp:
|
||||||
return true;
|
return true;
|
||||||
default:
|
default:
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
@@ -21,18 +21,18 @@
|
|||||||
|
|
||||||
#include <QJsonArray>
|
#include <QJsonArray>
|
||||||
|
|
||||||
#include "llmcore/PromptTemplate.hpp"
|
#include "pluginllmcore/PromptTemplate.hpp"
|
||||||
|
|
||||||
namespace QodeAssist::Templates {
|
namespace QodeAssist::Templates {
|
||||||
|
|
||||||
class LlamaCppFim : public LLMCore::PromptTemplate
|
class LlamaCppFim : public PluginLLMCore::PromptTemplate
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
LLMCore::TemplateType type() const override { return LLMCore::TemplateType::FIM; }
|
PluginLLMCore::TemplateType type() const override { return PluginLLMCore::TemplateType::FIM; }
|
||||||
QString name() const override { return "llama.cpp FIM"; }
|
QString name() const override { return "llama.cpp FIM"; }
|
||||||
QStringList stopWords() const override { return {}; }
|
QStringList stopWords() const override { return {}; }
|
||||||
|
|
||||||
void prepareRequest(QJsonObject &request, const LLMCore::ContextData &context) const override
|
void prepareRequest(QJsonObject &request, const PluginLLMCore::ContextData &context) const override
|
||||||
{
|
{
|
||||||
request["input_prefix"] = context.prefix.value_or("");
|
request["input_prefix"] = context.prefix.value_or("");
|
||||||
request["input_suffix"] = context.suffix.value_or("");
|
request["input_suffix"] = context.suffix.value_or("");
|
||||||
@@ -60,9 +60,9 @@ public:
|
|||||||
"Recommended for models with FIM capability.";
|
"Recommended for models with FIM capability.";
|
||||||
}
|
}
|
||||||
|
|
||||||
bool isSupportProvider(LLMCore::ProviderID id) const override
|
bool isSupportProvider(PluginLLMCore::ProviderID id) const override
|
||||||
{
|
{
|
||||||
return id == QodeAssist::LLMCore::ProviderID::LlamaCpp;
|
return id == QodeAssist::PluginLLMCore::ProviderID::LlamaCpp;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -21,17 +21,17 @@
|
|||||||
|
|
||||||
#include <QJsonArray>
|
#include <QJsonArray>
|
||||||
|
|
||||||
#include "llmcore/PromptTemplate.hpp"
|
#include "pluginllmcore/PromptTemplate.hpp"
|
||||||
|
|
||||||
namespace QodeAssist::Templates {
|
namespace QodeAssist::Templates {
|
||||||
|
|
||||||
class MistralAIFim : public LLMCore::PromptTemplate
|
class MistralAIFim : public PluginLLMCore::PromptTemplate
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
LLMCore::TemplateType type() const override { return LLMCore::TemplateType::FIM; }
|
PluginLLMCore::TemplateType type() const override { return PluginLLMCore::TemplateType::FIM; }
|
||||||
QString name() const override { return "Mistral AI FIM"; }
|
QString name() const override { return "Mistral AI FIM"; }
|
||||||
QStringList stopWords() const override { return QStringList(); }
|
QStringList stopWords() const override { return QStringList(); }
|
||||||
void prepareRequest(QJsonObject &request, const LLMCore::ContextData &context) const override
|
void prepareRequest(QJsonObject &request, const PluginLLMCore::ContextData &context) const override
|
||||||
{
|
{
|
||||||
request["prompt"] = context.prefix.value_or("");
|
request["prompt"] = context.prefix.value_or("");
|
||||||
request["suffix"] = context.suffix.value_or("");
|
request["suffix"] = context.suffix.value_or("");
|
||||||
@@ -45,10 +45,10 @@ public:
|
|||||||
"}\n\n"
|
"}\n\n"
|
||||||
"Optimized for code completion with MistralAI models.";
|
"Optimized for code completion with MistralAI models.";
|
||||||
}
|
}
|
||||||
bool isSupportProvider(LLMCore::ProviderID id) const override
|
bool isSupportProvider(PluginLLMCore::ProviderID id) const override
|
||||||
{
|
{
|
||||||
switch (id) {
|
switch (id) {
|
||||||
case QodeAssist::LLMCore::ProviderID::MistralAI:
|
case QodeAssist::PluginLLMCore::ProviderID::MistralAI:
|
||||||
return true;
|
return true;
|
||||||
default:
|
default:
|
||||||
return false;
|
return false;
|
||||||
@@ -56,14 +56,14 @@ public:
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
class MistralAIChat : public LLMCore::PromptTemplate
|
class MistralAIChat : public PluginLLMCore::PromptTemplate
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
LLMCore::TemplateType type() const override { return LLMCore::TemplateType::Chat; }
|
PluginLLMCore::TemplateType type() const override { return PluginLLMCore::TemplateType::Chat; }
|
||||||
QString name() const override { return "Mistral AI Chat"; }
|
QString name() const override { return "Mistral AI Chat"; }
|
||||||
QStringList stopWords() const override { return QStringList(); }
|
QStringList stopWords() const override { return QStringList(); }
|
||||||
|
|
||||||
void prepareRequest(QJsonObject &request, const LLMCore::ContextData &context) const override
|
void prepareRequest(QJsonObject &request, const PluginLLMCore::ContextData &context) const override
|
||||||
{
|
{
|
||||||
QJsonArray messages;
|
QJsonArray messages;
|
||||||
|
|
||||||
@@ -116,10 +116,10 @@ public:
|
|||||||
"}\n\n"
|
"}\n\n"
|
||||||
"Supports system messages, conversation history, and images.";
|
"Supports system messages, conversation history, and images.";
|
||||||
}
|
}
|
||||||
bool isSupportProvider(LLMCore::ProviderID id) const override
|
bool isSupportProvider(PluginLLMCore::ProviderID id) const override
|
||||||
{
|
{
|
||||||
switch (id) {
|
switch (id) {
|
||||||
case QodeAssist::LLMCore::ProviderID::MistralAI:
|
case QodeAssist::PluginLLMCore::ProviderID::MistralAI:
|
||||||
return true;
|
return true;
|
||||||
default:
|
default:
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
@@ -21,17 +21,17 @@
|
|||||||
|
|
||||||
#include <QJsonArray>
|
#include <QJsonArray>
|
||||||
|
|
||||||
#include "llmcore/PromptTemplate.hpp"
|
#include "pluginllmcore/PromptTemplate.hpp"
|
||||||
|
|
||||||
namespace QodeAssist::Templates {
|
namespace QodeAssist::Templates {
|
||||||
|
|
||||||
class OllamaFim : public LLMCore::PromptTemplate
|
class OllamaFim : public PluginLLMCore::PromptTemplate
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
LLMCore::TemplateType type() const override { return LLMCore::TemplateType::FIM; }
|
PluginLLMCore::TemplateType type() const override { return PluginLLMCore::TemplateType::FIM; }
|
||||||
QString name() const override { return "Ollama FIM"; }
|
QString name() const override { return "Ollama FIM"; }
|
||||||
QStringList stopWords() const override { return QStringList() << "<EOT>"; }
|
QStringList stopWords() const override { return QStringList() << "<EOT>"; }
|
||||||
void prepareRequest(QJsonObject &request, const LLMCore::ContextData &context) const override
|
void prepareRequest(QJsonObject &request, const PluginLLMCore::ContextData &context) const override
|
||||||
{
|
{
|
||||||
request["prompt"] = context.prefix.value_or("");
|
request["prompt"] = context.prefix.value_or("");
|
||||||
request["suffix"] = context.suffix.value_or("");
|
request["suffix"] = context.suffix.value_or("");
|
||||||
@@ -47,10 +47,10 @@ public:
|
|||||||
"}\n\n"
|
"}\n\n"
|
||||||
"Recommended for Ollama models with FIM capability.";
|
"Recommended for Ollama models with FIM capability.";
|
||||||
}
|
}
|
||||||
bool isSupportProvider(LLMCore::ProviderID id) const override
|
bool isSupportProvider(PluginLLMCore::ProviderID id) const override
|
||||||
{
|
{
|
||||||
switch (id) {
|
switch (id) {
|
||||||
case QodeAssist::LLMCore::ProviderID::Ollama:
|
case QodeAssist::PluginLLMCore::ProviderID::Ollama:
|
||||||
return true;
|
return true;
|
||||||
default:
|
default:
|
||||||
return false;
|
return false;
|
||||||
@@ -58,14 +58,14 @@ public:
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
class OllamaChat : public LLMCore::PromptTemplate
|
class OllamaChat : public PluginLLMCore::PromptTemplate
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
LLMCore::TemplateType type() const override { return LLMCore::TemplateType::Chat; }
|
PluginLLMCore::TemplateType type() const override { return PluginLLMCore::TemplateType::Chat; }
|
||||||
QString name() const override { return "Ollama Chat"; }
|
QString name() const override { return "Ollama Chat"; }
|
||||||
QStringList stopWords() const override { return QStringList(); }
|
QStringList stopWords() const override { return QStringList(); }
|
||||||
|
|
||||||
void prepareRequest(QJsonObject &request, const LLMCore::ContextData &context) const override
|
void prepareRequest(QJsonObject &request, const PluginLLMCore::ContextData &context) const override
|
||||||
{
|
{
|
||||||
QJsonArray messages;
|
QJsonArray messages;
|
||||||
|
|
||||||
@@ -107,10 +107,10 @@ public:
|
|||||||
"Recommended for Ollama models with chat capability.\n"
|
"Recommended for Ollama models with chat capability.\n"
|
||||||
"Supports images for multimodal models (e.g., llava).";
|
"Supports images for multimodal models (e.g., llava).";
|
||||||
}
|
}
|
||||||
bool isSupportProvider(LLMCore::ProviderID id) const override
|
bool isSupportProvider(PluginLLMCore::ProviderID id) const override
|
||||||
{
|
{
|
||||||
switch (id) {
|
switch (id) {
|
||||||
case QodeAssist::LLMCore::ProviderID::Ollama:
|
case QodeAssist::PluginLLMCore::ProviderID::Ollama:
|
||||||
return true;
|
return true;
|
||||||
default:
|
default:
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user