Files

40 lines
1.1 KiB
C++

#pragma once
#include <functional>
#include <string>
#include <expected>
#include <nlohmann/json.hpp>
#include "json_types.hpp"
// Template function that wraps a typed lambda for tool execution
// Handles JSON deserialization internally, eliminating std::any
template<typename ParamsType, typename F>
std::function<Content(const nlohmann::json&)> create_tool_execute(
F const& fn
) {
return [fn = std::move(fn)](const nlohmann::json& json) -> Content {
auto result = json_to<ParamsType>(json);
if (!result) {
return "Error: " + result.error();
}
return fn(result.value());
};
}
struct Tool {
std::string name;
std::string description;
nlohmann::json parameters_schema;
std::function<Content(const nlohmann::json&)> execute;
nlohmann::json to_api_json() const {
return {
{"type", "function"},
{"function", {
{"name", name},
{"description", description},
{"parameters", parameters_schema}
}}
};
}
};