Files
z-bar-qt/Plugins/ZShell/Llm/tool.hpp
T

71 lines
1.9 KiB
C++

#pragma once
#include <QJsonArray>
#include <QJsonObject>
#include <QList>
#include <QObject>
#include <QString>
#include <functional>
namespace ZShell::llm {
// A capability the model may invoke mid-turn. Tools run asynchronously
// and report exactly one result: `{"output": ...}` on success or
// `{"error": ...}` on failure.
class LlmTool : public QObject {
Q_OBJECT
public:
explicit LlmTool(QObject* parent = nullptr);
~LlmTool() override;
[[nodiscard]] virtual QString name() const = 0;
[[nodiscard]] virtual QString description() const = 0;
// JSON Schema describing the tool's `arguments` object.
[[nodiscard]] virtual QJsonObject parameters() const = 0;
// Runs the tool; `done` is invoked exactly once, with
// `{"output": ...}` on success or `{"error": ...}` on failure.
// `done` must be invoked asynchronously (on a later event loop
// iteration), never synchronously within execute().
virtual void execute(
const QJsonObject& args,
std::function<void(const QJsonObject& result)> done) = 0;
// Abandons in-flight work, if any.
virtual void cancel();
// The OpenAI-compatible `tools` entry for this tool.
[[nodiscard]] QJsonObject specification() const;
};
// Owns the set of tools available to the model.
class ToolRegistry : public QObject {
Q_OBJECT
Q_PROPERTY(bool enabled READ enabled WRITE setEnabled NOTIFY enabledChanged)
public:
explicit ToolRegistry(QObject* parent = nullptr);
[[nodiscard]] bool enabled() const { return m_enabled; }
void setEnabled(bool value);
// Takes ownership; tools become children of the registry.
void registerTool(LlmTool* tool);
[[nodiscard]] LlmTool* tool(const QString& name) const;
// The request body's `tools` array; empty while disabled.
[[nodiscard]] QJsonArray specifications() const;
// Abandons in-flight work in every tool.
void cancelAll();
Q_SIGNALS:
void enabledChanged();
private:
bool m_enabled = true;
QList<LlmTool*> m_tools;
};
} // namespace ZShell::llm