C++ / fmt (pull_request) Failing after 6s
C++ / build (pull_request) Failing after 10s
C++ / clang-tidy (pull_request) Failing after 9s
JS/TS / fmt (pull_request) Successful in 10s
JS/TS / lint (pull_request) Successful in 8s
Python / static (pull_request) Successful in 30s
Rust / build (pull_request) Successful in 49s
Python / verify (pull_request) Successful in 1m35s
Rust / fmt (pull_request) Successful in 36s
Rust / clippy (pull_request) Successful in 54s
88 lines
2.0 KiB
C++
88 lines
2.0 KiB
C++
#pragma once
|
|
|
|
#include <QJsonArray>
|
|
#include <QJsonObject>
|
|
#include <QList>
|
|
#include <QObject>
|
|
#include <QString>
|
|
|
|
#include <functional>
|
|
|
|
namespace ZShell::llm {
|
|
|
|
struct ToolResult {
|
|
bool ok = false;
|
|
QString text;
|
|
};
|
|
|
|
struct ToolContext {
|
|
QString toolCallId;
|
|
QString workspace;
|
|
int outputBudgetChars = 0;
|
|
};
|
|
|
|
using ToolDone = std::function<void(const ToolResult&)>;
|
|
|
|
class ToolRegistry;
|
|
|
|
class LlmTool : public QObject {
|
|
Q_OBJECT
|
|
|
|
public:
|
|
explicit LlmTool(ToolRegistry* registry, QObject* parent = nullptr);
|
|
~LlmTool() override;
|
|
|
|
[[nodiscard]] virtual QString name() const = 0;
|
|
[[nodiscard]] virtual QString description() const = 0;
|
|
[[nodiscard]] virtual QJsonObject parameters() const = 0;
|
|
|
|
[[nodiscard]] virtual bool available(const ToolContext& context) const;
|
|
|
|
virtual void execute(
|
|
const ToolContext& context, const QJsonObject& args, ToolDone done) = 0;
|
|
|
|
virtual void cancel();
|
|
|
|
[[nodiscard]] QJsonObject specification() const;
|
|
|
|
[[nodiscard]] ToolRegistry* registry() const { return m_registry; }
|
|
|
|
private:
|
|
ToolRegistry* m_registry;
|
|
};
|
|
|
|
class ToolRegistry : public QObject {
|
|
Q_OBJECT
|
|
|
|
public:
|
|
explicit ToolRegistry(QObject* parent = nullptr);
|
|
|
|
void registerTool(LlmTool* tool);
|
|
[[nodiscard]] LlmTool* tool(const QString& name) const;
|
|
[[nodiscard]] QList<LlmTool*> tools() const { return m_tools; }
|
|
[[nodiscard]] QJsonArray specifications(const ToolContext& context) const;
|
|
void cancelAll();
|
|
|
|
[[nodiscard]] bool enabled() const { return m_enabled; }
|
|
void setEnabled(bool value);
|
|
[[nodiscard]] bool sandboxEnabled() const { return m_sandbox; }
|
|
void setSandboxEnabled(bool value);
|
|
|
|
[[nodiscard]] int contextSize() const { return m_contextSize; }
|
|
void setContextSize(int value);
|
|
|
|
[[nodiscard]] int outputBudgetChars(int callCount) const;
|
|
|
|
static constexpr int CharsPerToken = 4;
|
|
static constexpr int DefaultInlineChars = 64 * 1024;
|
|
static constexpr int MinBudgetTokens = 512;
|
|
|
|
private:
|
|
bool m_enabled = true;
|
|
bool m_sandbox = true;
|
|
int m_contextSize = 0;
|
|
QList<LlmTool*> m_tools;
|
|
};
|
|
|
|
} // namespace ZShell::llm
|