mirror of
https://github.com/Palm1r/QodeAssist.git
synced 2025-12-11 03:42:53 -05:00
feat: Add tooling support to google provider (#237)
This commit is contained in:
@ -121,6 +121,7 @@ add_qtc_plugin(QodeAssist
|
|||||||
providers/ClaudeMessage.hpp providers/ClaudeMessage.cpp
|
providers/ClaudeMessage.hpp providers/ClaudeMessage.cpp
|
||||||
providers/OpenAIMessage.hpp providers/OpenAIMessage.cpp
|
providers/OpenAIMessage.hpp providers/OpenAIMessage.cpp
|
||||||
providers/OllamaMessage.hpp providers/OllamaMessage.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)
|
||||||
|
|||||||
@ -54,4 +54,20 @@ QJsonObject BaseTool::customizeForOllama(const QJsonObject &baseDefinition) cons
|
|||||||
return customizeForOpenAI(baseDefinition);
|
return customizeForOpenAI(baseDefinition);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QJsonObject BaseTool::customizeForGoogle(const QJsonObject &baseDefinition) const
|
||||||
|
{
|
||||||
|
QJsonObject functionDeclaration;
|
||||||
|
functionDeclaration["name"] = name();
|
||||||
|
functionDeclaration["description"] = description();
|
||||||
|
functionDeclaration["parameters"] = baseDefinition;
|
||||||
|
|
||||||
|
QJsonArray functionDeclarations;
|
||||||
|
functionDeclarations.append(functionDeclaration);
|
||||||
|
|
||||||
|
QJsonObject tool;
|
||||||
|
tool["function_declarations"] = functionDeclarations;
|
||||||
|
|
||||||
|
return tool;
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace QodeAssist::LLMCore
|
} // namespace QodeAssist::LLMCore
|
||||||
|
|||||||
@ -27,7 +27,7 @@
|
|||||||
|
|
||||||
namespace QodeAssist::LLMCore {
|
namespace QodeAssist::LLMCore {
|
||||||
|
|
||||||
enum class ToolSchemaFormat { OpenAI, Claude, Ollama };
|
enum class ToolSchemaFormat { OpenAI, Claude, Ollama, Google };
|
||||||
|
|
||||||
class BaseTool : public QObject
|
class BaseTool : public QObject
|
||||||
{
|
{
|
||||||
@ -47,6 +47,7 @@ protected:
|
|||||||
virtual QJsonObject customizeForOpenAI(const QJsonObject &baseDefinition) const;
|
virtual QJsonObject customizeForOpenAI(const QJsonObject &baseDefinition) const;
|
||||||
virtual QJsonObject customizeForClaude(const QJsonObject &baseDefinition) const;
|
virtual QJsonObject customizeForClaude(const QJsonObject &baseDefinition) const;
|
||||||
virtual QJsonObject customizeForOllama(const QJsonObject &baseDefinition) const;
|
virtual QJsonObject customizeForOllama(const QJsonObject &baseDefinition) const;
|
||||||
|
virtual QJsonObject customizeForGoogle(const QJsonObject &baseDefinition) const;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace QodeAssist::LLMCore
|
} // namespace QodeAssist::LLMCore
|
||||||
|
|||||||
@ -34,6 +34,17 @@
|
|||||||
|
|
||||||
namespace QodeAssist::Providers {
|
namespace QodeAssist::Providers {
|
||||||
|
|
||||||
|
GoogleAIProvider::GoogleAIProvider(QObject *parent)
|
||||||
|
: LLMCore::Provider(parent)
|
||||||
|
, m_toolsManager(new Tools::ToolsManager(this))
|
||||||
|
{
|
||||||
|
connect(
|
||||||
|
m_toolsManager,
|
||||||
|
&Tools::ToolsManager::toolExecutionComplete,
|
||||||
|
this,
|
||||||
|
&GoogleAIProvider::onToolExecutionComplete);
|
||||||
|
}
|
||||||
|
|
||||||
QString GoogleAIProvider::name() const
|
QString GoogleAIProvider::name() const
|
||||||
{
|
{
|
||||||
return "Google AI";
|
return "Google AI";
|
||||||
@ -89,6 +100,16 @@ void GoogleAIProvider::prepareRequest(
|
|||||||
} else {
|
} else {
|
||||||
applyModelParams(Settings::chatAssistantSettings());
|
applyModelParams(Settings::chatAssistantSettings());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (supportsTools() && type == LLMCore::RequestType::Chat
|
||||||
|
&& Settings::chatAssistantSettings().useTools()) {
|
||||||
|
auto toolsDefinitions = m_toolsManager->getToolsDefinitions(
|
||||||
|
LLMCore::ToolSchemaFormat::Google);
|
||||||
|
if (!toolsDefinitions.isEmpty()) {
|
||||||
|
request["tools"] = toolsDefinitions;
|
||||||
|
LOG_MESSAGE(QString("Added %1 tools to Google AI request").arg(toolsDefinitions.size()));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
QList<QString> GoogleAIProvider::getInstalledModels(const QString &url)
|
QList<QString> GoogleAIProvider::getInstalledModels(const QString &url)
|
||||||
@ -143,7 +164,8 @@ QList<QString> GoogleAIProvider::validateRequest(
|
|||||||
{"system_instruction", QJsonArray{}},
|
{"system_instruction", QJsonArray{}},
|
||||||
{"generationConfig",
|
{"generationConfig",
|
||||||
QJsonObject{{"temperature", {}}, {"maxOutputTokens", {}}, {"topP", {}}, {"topK", {}}}},
|
QJsonObject{{"temperature", {}}, {"maxOutputTokens", {}}, {"topP", {}}, {"topK", {}}}},
|
||||||
{"safetySettings", QJsonArray{}}};
|
{"safetySettings", QJsonArray{}},
|
||||||
|
{"tools", QJsonArray{}}};
|
||||||
|
|
||||||
return LLMCore::ValidationUtils::validateRequestFields(request, templateReq);
|
return LLMCore::ValidationUtils::validateRequestFields(request, templateReq);
|
||||||
}
|
}
|
||||||
@ -172,8 +194,12 @@ LLMCore::ProviderID GoogleAIProvider::providerID() const
|
|||||||
void GoogleAIProvider::sendRequest(
|
void GoogleAIProvider::sendRequest(
|
||||||
const LLMCore::RequestID &requestId, const QUrl &url, const QJsonObject &payload)
|
const LLMCore::RequestID &requestId, const QUrl &url, const QJsonObject &payload)
|
||||||
{
|
{
|
||||||
m_dataBuffers[requestId].clear();
|
if (!m_messages.contains(requestId)) {
|
||||||
|
m_dataBuffers[requestId].clear();
|
||||||
|
}
|
||||||
|
|
||||||
m_requestUrls[requestId] = url;
|
m_requestUrls[requestId] = url;
|
||||||
|
m_originalRequests[requestId] = payload;
|
||||||
|
|
||||||
QNetworkRequest networkRequest(url);
|
QNetworkRequest networkRequest(url);
|
||||||
prepareNetworkRequest(networkRequest);
|
prepareNetworkRequest(networkRequest);
|
||||||
@ -187,6 +213,18 @@ void GoogleAIProvider::sendRequest(
|
|||||||
emit httpClient()->sendRequest(request);
|
emit httpClient()->sendRequest(request);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool GoogleAIProvider::supportsTools() 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(
|
void GoogleAIProvider::onDataReceived(
|
||||||
const QodeAssist::LLMCore::RequestID &requestId, const QByteArray &data)
|
const QodeAssist::LLMCore::RequestID &requestId, const QByteArray &data)
|
||||||
{
|
{
|
||||||
@ -207,17 +245,24 @@ void GoogleAIProvider::onDataReceived(
|
|||||||
|
|
||||||
LOG_MESSAGE(fullError);
|
LOG_MESSAGE(fullError);
|
||||||
emit requestFailed(requestId, fullError);
|
emit requestFailed(requestId, fullError);
|
||||||
m_dataBuffers.remove(requestId);
|
cleanupRequest(requestId);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool isDone = handleStreamResponse(requestId, data);
|
LLMCore::DataBuffers &buffers = m_dataBuffers[requestId];
|
||||||
|
QStringList lines = buffers.rawStreamBuffer.processData(data);
|
||||||
|
|
||||||
if (isDone) {
|
for (const QString &line : lines) {
|
||||||
LLMCore::DataBuffers &buffers = m_dataBuffers[requestId];
|
if (line.trimmed().isEmpty()) {
|
||||||
emit fullResponseReceived(requestId, buffers.responseContent);
|
continue;
|
||||||
m_dataBuffers.remove(requestId);
|
}
|
||||||
|
|
||||||
|
QJsonObject chunk = parseEventLine(line);
|
||||||
|
if (chunk.isEmpty())
|
||||||
|
continue;
|
||||||
|
|
||||||
|
processStreamChunk(requestId, chunk);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -227,67 +272,179 @@ void GoogleAIProvider::onRequestFinished(
|
|||||||
if (!success) {
|
if (!success) {
|
||||||
LOG_MESSAGE(QString("GoogleAIProvider request %1 failed: %2").arg(requestId, error));
|
LOG_MESSAGE(QString("GoogleAIProvider request %1 failed: %2").arg(requestId, error));
|
||||||
emit requestFailed(requestId, error);
|
emit requestFailed(requestId, error);
|
||||||
} else {
|
cleanupRequest(requestId);
|
||||||
if (m_dataBuffers.contains(requestId)) {
|
return;
|
||||||
const LLMCore::DataBuffers &buffers = m_dataBuffers[requestId];
|
}
|
||||||
if (!buffers.responseContent.isEmpty()) {
|
|
||||||
emit fullResponseReceived(requestId, buffers.responseContent);
|
if (m_messages.contains(requestId)) {
|
||||||
|
GoogleMessage *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 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
LOG_MESSAGE(QString("Tool execution complete for Google AI request %1").arg(requestId));
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
LOG_MESSAGE(QString("Sending continuation request for %1 with %2 tool results")
|
||||||
|
.arg(requestId)
|
||||||
|
.arg(toolResults.size()));
|
||||||
|
|
||||||
|
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();
|
||||||
|
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();
|
||||||
|
message->handleContentDelta(text);
|
||||||
|
|
||||||
|
LLMCore::DataBuffers &buffers = m_dataBuffers[requestId];
|
||||||
|
buffers.responseContent += text;
|
||||||
|
emit partialResponseReceived(requestId, text);
|
||||||
|
} else if (partObj.contains("functionCall")) {
|
||||||
|
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);
|
||||||
|
handleMessageComplete(requestId);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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_dataBuffers.remove(requestId);
|
||||||
m_requestUrls.remove(requestId);
|
m_requestUrls.remove(requestId);
|
||||||
}
|
m_originalRequests.remove(requestId);
|
||||||
|
m_toolsManager->cleanupRequest(requestId);
|
||||||
bool GoogleAIProvider::handleStreamResponse(
|
|
||||||
const LLMCore::RequestID &requestId, const QByteArray &data)
|
|
||||||
{
|
|
||||||
LLMCore::DataBuffers &buffers = m_dataBuffers[requestId];
|
|
||||||
QStringList lines = buffers.rawStreamBuffer.processData(data);
|
|
||||||
|
|
||||||
bool isDone = false;
|
|
||||||
QString tempResponse;
|
|
||||||
|
|
||||||
for (const QString &line : lines) {
|
|
||||||
if (line.trimmed().isEmpty()) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
QJsonObject responseObj = parseEventLine(line);
|
|
||||||
if (responseObj.isEmpty())
|
|
||||||
continue;
|
|
||||||
|
|
||||||
if (responseObj.contains("candidates")) {
|
|
||||||
QJsonArray candidates = responseObj["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")) {
|
|
||||||
tempResponse += partObj["text"].toString();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (candidateObj.contains("finishReason")) {
|
|
||||||
isDone = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!tempResponse.isEmpty()) {
|
|
||||||
buffers.responseContent += tempResponse;
|
|
||||||
emit partialResponseReceived(requestId, tempResponse);
|
|
||||||
}
|
|
||||||
|
|
||||||
return isDone;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace QodeAssist::Providers
|
} // namespace QodeAssist::Providers
|
||||||
|
|||||||
@ -19,13 +19,18 @@
|
|||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include "GoogleMessage.hpp"
|
||||||
#include "llmcore/Provider.hpp"
|
#include "llmcore/Provider.hpp"
|
||||||
|
#include "tools/ToolsManager.hpp"
|
||||||
|
|
||||||
namespace QodeAssist::Providers {
|
namespace QodeAssist::Providers {
|
||||||
|
|
||||||
class GoogleAIProvider : public LLMCore::Provider
|
class GoogleAIProvider : public LLMCore::Provider
|
||||||
{
|
{
|
||||||
|
Q_OBJECT
|
||||||
public:
|
public:
|
||||||
|
explicit GoogleAIProvider(QObject *parent = nullptr);
|
||||||
|
|
||||||
QString name() const override;
|
QString name() const override;
|
||||||
QString url() const override;
|
QString url() const override;
|
||||||
QString completionEndpoint() const override;
|
QString completionEndpoint() const override;
|
||||||
@ -45,6 +50,9 @@ public:
|
|||||||
void sendRequest(
|
void sendRequest(
|
||||||
const LLMCore::RequestID &requestId, const QUrl &url, const QJsonObject &payload) override;
|
const LLMCore::RequestID &requestId, const QUrl &url, const QJsonObject &payload) override;
|
||||||
|
|
||||||
|
bool supportsTools() const override;
|
||||||
|
void cancelRequest(const LLMCore::RequestID &requestId) override;
|
||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
void onDataReceived(
|
void onDataReceived(
|
||||||
const QodeAssist::LLMCore::RequestID &requestId, const QByteArray &data) override;
|
const QodeAssist::LLMCore::RequestID &requestId, const QByteArray &data) override;
|
||||||
@ -53,8 +61,19 @@ public slots:
|
|||||||
bool success,
|
bool success,
|
||||||
const QString &error) override;
|
const QString &error) override;
|
||||||
|
|
||||||
|
private slots:
|
||||||
|
void onToolExecutionComplete(
|
||||||
|
const QString &requestId, const QHash<QString, QString> &toolResults);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool handleStreamResponse(const LLMCore::RequestID &requestId, const QByteArray &data);
|
void processStreamChunk(const QString &requestId, const QJsonObject &chunk);
|
||||||
|
void handleMessageComplete(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;
|
||||||
|
Tools::ToolsManager *m_toolsManager;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace QodeAssist::Providers
|
} // namespace QodeAssist::Providers
|
||||||
|
|||||||
173
providers/GoogleMessage.cpp
Normal file
173
providers/GoogleMessage.cpp
Normal file
@ -0,0 +1,173 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 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 "GoogleMessage.hpp"
|
||||||
|
|
||||||
|
#include <QJsonDocument>
|
||||||
|
#include <QUuid>
|
||||||
|
|
||||||
|
#include "logger/Logger.hpp"
|
||||||
|
|
||||||
|
namespace QodeAssist::Providers {
|
||||||
|
|
||||||
|
GoogleMessage::GoogleMessage(QObject *parent)
|
||||||
|
: QObject(parent)
|
||||||
|
{}
|
||||||
|
|
||||||
|
void GoogleMessage::handleContentDelta(const QString &text)
|
||||||
|
{
|
||||||
|
if (m_currentBlocks.isEmpty() || !qobject_cast<LLMCore::TextContent *>(m_currentBlocks.last())) {
|
||||||
|
auto textContent = new LLMCore::TextContent();
|
||||||
|
textContent->setParent(this);
|
||||||
|
m_currentBlocks.append(textContent);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (auto textContent = qobject_cast<LLMCore::TextContent *>(m_currentBlocks.last())) {
|
||||||
|
textContent->appendText(text);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void GoogleMessage::handleFunctionCallStart(const QString &name)
|
||||||
|
{
|
||||||
|
m_currentFunctionName = name;
|
||||||
|
m_pendingFunctionArgs.clear();
|
||||||
|
|
||||||
|
LOG_MESSAGE(QString("Google: Starting function call: %1").arg(name));
|
||||||
|
}
|
||||||
|
|
||||||
|
void GoogleMessage::handleFunctionCallArgsDelta(const QString &argsJson)
|
||||||
|
{
|
||||||
|
m_pendingFunctionArgs += argsJson;
|
||||||
|
}
|
||||||
|
|
||||||
|
void GoogleMessage::handleFunctionCallComplete()
|
||||||
|
{
|
||||||
|
if (m_currentFunctionName.isEmpty()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
QJsonObject args;
|
||||||
|
if (!m_pendingFunctionArgs.isEmpty()) {
|
||||||
|
QJsonDocument doc = QJsonDocument::fromJson(m_pendingFunctionArgs.toUtf8());
|
||||||
|
if (doc.isObject()) {
|
||||||
|
args = doc.object();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
QString id = QUuid::createUuid().toString(QUuid::WithoutBraces);
|
||||||
|
auto toolContent = new LLMCore::ToolUseContent(id, m_currentFunctionName, args);
|
||||||
|
toolContent->setParent(this);
|
||||||
|
m_currentBlocks.append(toolContent);
|
||||||
|
|
||||||
|
LOG_MESSAGE(QString("Google: Completed function call: name=%1, args=%2")
|
||||||
|
.arg(m_currentFunctionName)
|
||||||
|
.arg(QString::fromUtf8(QJsonDocument(args).toJson(QJsonDocument::Compact))));
|
||||||
|
|
||||||
|
m_currentFunctionName.clear();
|
||||||
|
m_pendingFunctionArgs.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
void GoogleMessage::handleFinishReason(const QString &reason)
|
||||||
|
{
|
||||||
|
m_finishReason = reason;
|
||||||
|
updateStateFromFinishReason();
|
||||||
|
|
||||||
|
LOG_MESSAGE(
|
||||||
|
QString("Google: Finish reason: %1, state: %2").arg(reason).arg(static_cast<int>(m_state)));
|
||||||
|
}
|
||||||
|
|
||||||
|
QJsonObject GoogleMessage::toProviderFormat() const
|
||||||
|
{
|
||||||
|
QJsonObject content;
|
||||||
|
content["role"] = "model";
|
||||||
|
|
||||||
|
QJsonArray parts;
|
||||||
|
|
||||||
|
for (auto block : m_currentBlocks) {
|
||||||
|
if (!block)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
if (auto text = qobject_cast<LLMCore::TextContent *>(block)) {
|
||||||
|
parts.append(QJsonObject{{"text", text->text()}});
|
||||||
|
} else if (auto tool = qobject_cast<LLMCore::ToolUseContent *>(block)) {
|
||||||
|
QJsonObject functionCall;
|
||||||
|
functionCall["name"] = tool->name();
|
||||||
|
functionCall["args"] = tool->input();
|
||||||
|
parts.append(QJsonObject{{"functionCall", functionCall}});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
content["parts"] = parts;
|
||||||
|
return content;
|
||||||
|
}
|
||||||
|
|
||||||
|
QJsonArray GoogleMessage::createToolResultParts(const QHash<QString, QString> &toolResults) const
|
||||||
|
{
|
||||||
|
QJsonArray parts;
|
||||||
|
|
||||||
|
for (auto toolContent : getCurrentToolUseContent()) {
|
||||||
|
if (toolResults.contains(toolContent->id())) {
|
||||||
|
QJsonObject functionResponse;
|
||||||
|
functionResponse["name"] = toolContent->name();
|
||||||
|
|
||||||
|
QJsonObject response;
|
||||||
|
response["result"] = toolResults[toolContent->id()];
|
||||||
|
functionResponse["response"] = response;
|
||||||
|
|
||||||
|
parts.append(QJsonObject{{"functionResponse", functionResponse}});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return parts;
|
||||||
|
}
|
||||||
|
|
||||||
|
QList<LLMCore::ToolUseContent *> GoogleMessage::getCurrentToolUseContent() const
|
||||||
|
{
|
||||||
|
QList<LLMCore::ToolUseContent *> toolBlocks;
|
||||||
|
for (auto block : m_currentBlocks) {
|
||||||
|
if (auto toolContent = qobject_cast<LLMCore::ToolUseContent *>(block)) {
|
||||||
|
toolBlocks.append(toolContent);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return toolBlocks;
|
||||||
|
}
|
||||||
|
|
||||||
|
void GoogleMessage::startNewContinuation()
|
||||||
|
{
|
||||||
|
LOG_MESSAGE(QString("GoogleMessage: Starting new continuation"));
|
||||||
|
|
||||||
|
m_currentBlocks.clear();
|
||||||
|
m_pendingFunctionArgs.clear();
|
||||||
|
m_currentFunctionName.clear();
|
||||||
|
m_finishReason.clear();
|
||||||
|
m_state = LLMCore::MessageState::Building;
|
||||||
|
}
|
||||||
|
|
||||||
|
void GoogleMessage::updateStateFromFinishReason()
|
||||||
|
{
|
||||||
|
if (m_finishReason == "STOP" || m_finishReason == "MAX_TOKENS") {
|
||||||
|
m_state = getCurrentToolUseContent().isEmpty()
|
||||||
|
? LLMCore::MessageState::Complete
|
||||||
|
: LLMCore::MessageState::RequiresToolExecution;
|
||||||
|
} else {
|
||||||
|
m_state = LLMCore::MessageState::Complete;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace QodeAssist::Providers
|
||||||
62
providers/GoogleMessage.hpp
Normal file
62
providers/GoogleMessage.hpp
Normal file
@ -0,0 +1,62 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (C) 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 <QHash>
|
||||||
|
#include <QJsonArray>
|
||||||
|
#include <QJsonObject>
|
||||||
|
#include <QObject>
|
||||||
|
|
||||||
|
#include <llmcore/ContentBlocks.hpp>
|
||||||
|
|
||||||
|
namespace QodeAssist::Providers {
|
||||||
|
|
||||||
|
class GoogleMessage : public QObject
|
||||||
|
{
|
||||||
|
Q_OBJECT
|
||||||
|
public:
|
||||||
|
explicit GoogleMessage(QObject *parent = nullptr);
|
||||||
|
|
||||||
|
void handleContentDelta(const QString &text);
|
||||||
|
void handleFunctionCallStart(const QString &name);
|
||||||
|
void handleFunctionCallArgsDelta(const QString &argsJson);
|
||||||
|
void handleFunctionCallComplete();
|
||||||
|
void handleFinishReason(const QString &reason);
|
||||||
|
|
||||||
|
QJsonObject toProviderFormat() const;
|
||||||
|
QJsonArray createToolResultParts(const QHash<QString, QString> &toolResults) const;
|
||||||
|
|
||||||
|
QList<LLMCore::ToolUseContent *> getCurrentToolUseContent() const;
|
||||||
|
QList<LLMCore::ContentBlock *> currentBlocks() const { return m_currentBlocks; }
|
||||||
|
|
||||||
|
LLMCore::MessageState state() const { return m_state; }
|
||||||
|
void startNewContinuation();
|
||||||
|
|
||||||
|
private:
|
||||||
|
void updateStateFromFinishReason();
|
||||||
|
|
||||||
|
QList<LLMCore::ContentBlock *> m_currentBlocks;
|
||||||
|
QString m_pendingFunctionArgs;
|
||||||
|
QString m_currentFunctionName;
|
||||||
|
QString m_finishReason;
|
||||||
|
LLMCore::MessageState m_state = LLMCore::MessageState::Building;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace QodeAssist::Providers
|
||||||
@ -68,6 +68,8 @@ QJsonObject ListProjectFilesTool::getDefinition(LLMCore::ToolSchemaFormat format
|
|||||||
return customizeForClaude(definition);
|
return customizeForClaude(definition);
|
||||||
case LLMCore::ToolSchemaFormat::Ollama:
|
case LLMCore::ToolSchemaFormat::Ollama:
|
||||||
return customizeForOllama(definition);
|
return customizeForOllama(definition);
|
||||||
|
case LLMCore::ToolSchemaFormat::Google:
|
||||||
|
return customizeForGoogle(definition);
|
||||||
}
|
}
|
||||||
|
|
||||||
return definition;
|
return definition;
|
||||||
|
|||||||
@ -86,6 +86,8 @@ QJsonObject ReadProjectFileByNameTool::getDefinition(LLMCore::ToolSchemaFormat f
|
|||||||
return customizeForClaude(definition);
|
return customizeForClaude(definition);
|
||||||
case LLMCore::ToolSchemaFormat::Ollama:
|
case LLMCore::ToolSchemaFormat::Ollama:
|
||||||
return customizeForOllama(definition);
|
return customizeForOllama(definition);
|
||||||
|
case LLMCore::ToolSchemaFormat::Google:
|
||||||
|
return customizeForGoogle(definition);
|
||||||
}
|
}
|
||||||
|
|
||||||
return definition;
|
return definition;
|
||||||
|
|||||||
@ -66,6 +66,8 @@ QJsonObject ReadVisibleFilesTool::getDefinition(LLMCore::ToolSchemaFormat format
|
|||||||
return customizeForClaude(definition);
|
return customizeForClaude(definition);
|
||||||
case LLMCore::ToolSchemaFormat::Ollama:
|
case LLMCore::ToolSchemaFormat::Ollama:
|
||||||
return customizeForOllama(definition);
|
return customizeForOllama(definition);
|
||||||
|
case LLMCore::ToolSchemaFormat::Google:
|
||||||
|
return customizeForGoogle(definition);
|
||||||
}
|
}
|
||||||
|
|
||||||
return definition;
|
return definition;
|
||||||
|
|||||||
Reference in New Issue
Block a user