feat: Add tools, fabric and executable handler (#230)

This commit is contained in:
Petr Mironychev
2025-09-22 12:36:13 +02:00
committed by GitHub
parent 5cde6ffac7
commit d0f8c1098f
12 changed files with 679 additions and 25 deletions

88
llmcore/BaseTool.cpp Normal file
View File

@ -0,0 +1,88 @@
/*
* 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 "BaseTool.hpp"
namespace QodeAssist::LLMCore {
BaseTool::BaseTool(QObject *parent)
: QObject(parent)
{}
QJsonObject BaseTool::getDefinition(ToolSchemaFormat format) const
{
QJsonObject properties;
QJsonObject filenameProperty;
filenameProperty["type"] = "string";
filenameProperty["description"] = "The filename or relative path to read";
properties["filename"] = filenameProperty;
QJsonObject definition;
definition["type"] = "object";
definition["properties"] = properties;
QJsonArray required;
required.append("filename");
definition["required"] = required;
switch (format) {
case LLMCore::ToolSchemaFormat::OpenAI:
definition = customizeForOpenAI(definition);
break;
case LLMCore::ToolSchemaFormat::Claude:
definition = customizeForClaude(definition);
break;
case LLMCore::ToolSchemaFormat::Ollama:
definition = customizeForOllama(definition);
break;
}
return definition;
}
QJsonObject BaseTool::customizeForOpenAI(const QJsonObject &baseDefinition) const
{
QJsonObject function;
function["name"] = name();
function["description"] = description();
function["parameters"] = baseDefinition;
QJsonObject tool;
tool["type"] = "function";
tool["function"] = function;
return tool;
}
QJsonObject BaseTool::customizeForClaude(const QJsonObject &baseDefinition) const
{
QJsonObject tool;
tool["name"] = name();
tool["description"] = description();
tool["input_schema"] = baseDefinition;
return tool;
}
QJsonObject BaseTool::customizeForOllama(const QJsonObject &baseDefinition) const
{
return customizeForOpenAI(baseDefinition);
}
} // namespace QodeAssist::LLMCore

View File

@ -19,31 +19,33 @@
#pragma once
#include <QFuture>
#include <QJsonArray>
#include <QJsonObject>
#include <QObject>
#include <QString>
namespace QodeAssist::LLMCore {
class ITool : public QObject
enum class ToolSchemaFormat { OpenAI, Claude, Ollama };
class BaseTool : public QObject
{
Q_OBJECT
public:
explicit ITool(QObject *parent = nullptr)
: QObject(parent)
{}
virtual ~ITool() = default;
explicit BaseTool(QObject *parent = nullptr);
~BaseTool() override = default;
virtual QString name() const = 0;
virtual QString description() const = 0;
virtual QJsonObject getDefinition() const = 0;
virtual QString execute(const QJsonObject &input = QJsonObject()) = 0;
virtual QJsonObject getDefinition(ToolSchemaFormat format) const;
signals:
void toolStarted(const QString &toolName);
void toolCompleted(const QString &toolName, const QString &result);
void toolFailed(const QString &toolName, const QString &error);
virtual QFuture<QString> executeAsync(const QJsonObject &input = QJsonObject()) = 0;
protected:
virtual QJsonObject customizeForOpenAI(const QJsonObject &baseDefinition) const;
virtual QJsonObject customizeForClaude(const QJsonObject &baseDefinition) const;
virtual QJsonObject customizeForOllama(const QJsonObject &baseDefinition) const;
};
} // namespace QodeAssist::LLMCore

View File

@ -17,8 +17,8 @@ add_library(LLMCore STATIC
HttpClient.hpp HttpClient.cpp
DataBuffers.hpp
SSEBuffer.hpp SSEBuffer.cpp
ITool.hpp
IToolsFactory.hpp
BaseTool.hpp
BaseTool.cpp
)
target_link_libraries(LLMCore

View File

@ -1,38 +0,0 @@
/*
* 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 <QJsonArray>
#include <QList>
#include "ITool.hpp"
namespace QodeAssist::LLMCore {
class IToolsFactory
{
public:
virtual ~IToolsFactory() = default;
virtual QList<ITool *> getAvailableTools() const = 0;
virtual ITool *getToolByName(const QString &name) const = 0;
virtual QJsonArray getToolsDefinitions() const = 0;
};
} // namespace QodeAssist::LLMCore