ForestHub SDK 0.1.0
C++14 LLM SDK for PC and embedded platforms
Loading...
Searching...
No Matches
tools.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: AGPL-3.0-only
2// Copyright (c) 2026 ForestHub. All rights reserved.
3// For commercial licensing, visit https://github.com/ForestHubAI/fh-sdk
4
5#ifndef FORESTHUB_CORE_TOOLS_HPP
6#define FORESTHUB_CORE_TOOLS_HPP
7
10
11#include <cstdint>
12#include <functional>
13#include <memory>
14#include <string>
15#include <utility>
16
20
21namespace foresthub {
22namespace core {
23
24using json = nlohmann::json;
25
26// 1. Interfaces
27
29enum class ToolType : uint8_t {
33};
34
36class Tool {
37public:
38 virtual ~Tool() = default;
39
41 virtual std::string ToolName() const = 0;
42
44 virtual ToolType GetToolType() const = 0;
45
48 virtual void ToJson(json& j) const = 0;
49
52 virtual bool IsExternal() const { return false; }
53};
54
57public:
58 virtual ~InternalToolCall() = default;
59
61 virtual std::string ToolName() const = 0;
62};
63
64// 2. Data Structures
65
67class ToolCallRequest : public InputItem {
68public:
69 std::string call_id;
70 std::string name;
71 std::string arguments;
72
75
77 std::string ToString() const override { return "Function tool call: " + name + " with arguments " + arguments; }
78
80 void ToJson(json& j) const override {
81 j = json{{"callId", call_id}, {"name", name}};
82 json args_json = json::parse(arguments, nullptr, false);
83 if (!args_json.is_discarded()) {
84 j["arguments"] = args_json;
85 } else {
86 j["arguments"] = arguments;
87 }
88 }
89};
90
92class ToolResult : public InputItem {
93public:
94 std::string call_id;
95 std::string name;
96 json output;
97
100
102 std::string ToString() const override {
103 if (output.is_string()) {
104 return "Function " + name + " returned: " + output.get<std::string>();
105 }
106 return "Function " + name + " returned: " + output.dump();
107 }
108
110 void ToJson(json& j) const override {
111 j = json{{"callId", call_id}, {"name", name}};
112 j["output"] = output;
113 }
114};
115
116// 3. External Tools
117
119class ExternalTool : public Tool {
120public:
122 virtual std::string ToolDescription() const = 0;
123
125 virtual const json& ToolParameters() const = 0;
126
128 bool IsExternal() const override { return true; }
129};
130
133public:
134 std::string name;
135 std::string description;
137
139 std::string ToolName() const override { return name; }
141 std::string ToolDescription() const override { return description; }
143 const json& ToolParameters() const override { return parameters; }
144
146 void ToJson(json& j) const override {
147 j = json{{"type", "external"}, {"name", name}, {"description", description}, {"parameters", parameters}};
148 }
149};
150
153public:
154 std::function<json(const std::string&)>
157 ToolType GetToolType() const override { return ToolType::kFunction; }
158};
159
160// 4. Factory Function
161
172template <typename T, typename R>
173std::shared_ptr<FunctionTool> NewFunctionTool(std::string name, std::string description, const json& schema,
174 const std::function<R(T)>& handler) {
175 auto tool = std::make_shared<FunctionTool>();
176
177 tool->name = std::move(name);
178 tool->description = std::move(description);
179 tool->parameters = util::NormalizeSchema(schema);
180
181 tool->tool_call = [handler, tool_name = tool->name](const std::string& raw_args) -> json {
182 json j_args = json::parse(raw_args, nullptr, false);
183 if (j_args.is_discarded()) {
184 return json(std::string("Error: Tool arguments for " + tool_name + " were not valid JSON."));
185 }
186
187 T args = j_args.get<T>();
188
189 R result = handler(args);
190 return result;
191 };
192
193 return tool;
194}
195
196// 5. Internal Tools
197
199class WebSearch : public Tool {
200public:
202 std::string ToolName() const override { return "web_search"; }
204 ToolType GetToolType() const override { return ToolType::kWebSearch; }
205
207 void ToJson(json& j) const override { j = json{{"type", "web_search"}}; }
208};
209
212public:
213 std::string query;
215 std::string ToolName() const override { return "web_search"; }
216};
217
218} // namespace core
219} // namespace foresthub
220
221#endif // FORESTHUB_CORE_TOOLS_HPP
Base implementation of ExternalTool with name, description, and parameters.
Definition tools.hpp:132
std::string ToolName() const override
Returns the stored name field.
Definition tools.hpp:139
void ToJson(json &j) const override
Serialize external tool definition to JSON.
Definition tools.hpp:146
std::string ToolDescription() const override
Returns the stored description field.
Definition tools.hpp:141
const json & ToolParameters() const override
Returns the stored parameters field.
Definition tools.hpp:143
std::string description
Human-readable description sent to the LLM.
Definition tools.hpp:135
json parameters
JSON schema defining expected parameters.
Definition tools.hpp:136
std::string name
Tool name sent to the LLM.
Definition tools.hpp:134
Tool with description and parameter schema, handled outside the LLM provider.
Definition tools.hpp:119
virtual const json & ToolParameters() const =0
JSON schema defining the expected parameters for this tool.
bool IsExternal() const override
Always returns true — this type is an ExternalTool.
Definition tools.hpp:128
virtual std::string ToolDescription() const =0
Human-readable description of this tool's purpose, sent to the LLM in the tool schema.
ExternalTool with a C++ callback for execution.
Definition tools.hpp:152
ToolType GetToolType() const override
Identifies this tool as a user-defined function.
Definition tools.hpp:157
std::function< json(const std::string &)> tool_call
Callback invoked with raw JSON arguments; returns a json value used as ToolResult::output.
Definition tools.hpp:155
Abstract base for items inside an InputItems list.
Definition input.hpp:51
Represents a tool call initiated internally by the model.
Definition tools.hpp:56
virtual std::string ToolName() const =0
Returns the name of the internal tool that was called.
Request from the model to call a tool.
Definition tools.hpp:67
void ToJson(json &j) const override
Serialize this tool call request to JSON.
Definition tools.hpp:80
std::string call_id
Provider-assigned call identifier for matching results.
Definition tools.hpp:69
InputItemType GetItemType() const override
Identifies this item as a tool call request.
Definition tools.hpp:74
std::string ToString() const override
Human-readable summary of this tool call.
Definition tools.hpp:77
std::string arguments
Raw JSON string.
Definition tools.hpp:71
std::string name
Name of the tool to invoke.
Definition tools.hpp:70
Output of an executed tool.
Definition tools.hpp:92
std::string ToString() const override
Human-readable summary of the tool output.
Definition tools.hpp:102
json output
Tool return value (string or structured JSON).
Definition tools.hpp:96
std::string name
Name of the tool that produced this result.
Definition tools.hpp:95
void ToJson(json &j) const override
Serialize this tool result to JSON.
Definition tools.hpp:110
std::string call_id
Matching call identifier from the request.
Definition tools.hpp:94
InputItemType GetItemType() const override
Identifies this item as a tool execution result.
Definition tools.hpp:99
Base interface for all tools.
Definition tools.hpp:36
virtual bool IsExternal() const
Check if this tool is an ExternalTool (enables static_pointer_cast<ExternalTool>).
Definition tools.hpp:52
virtual ToolType GetToolType() const =0
Returns the type discriminator for safe downcasting via static_pointer_cast.
virtual std::string ToolName() const =0
Returns the unique name identifying this tool in the LLM schema.
virtual void ToJson(json &j) const =0
Serialize this tool to JSON.
Log entry for a web search invocation.
Definition tools.hpp:211
std::string query
Search query issued by the model.
Definition tools.hpp:213
std::string ToolName() const override
Always returns "web_search".
Definition tools.hpp:215
Built-in web search tool.
Definition tools.hpp:199
ToolType GetToolType() const override
Identifies this as a web search tool.
Definition tools.hpp:204
std::string ToolName() const override
Always returns "web_search".
Definition tools.hpp:202
void ToJson(json &j) const override
Serialize web search tool to JSON.
Definition tools.hpp:207
Polymorphic input types for chat requests.
Wrapper for nlohmann/json that works around abs macro conflicts.
Core abstractions: requests, responses, tools, input types, and provider interface.
ToolType
Type discriminator for Tool subclasses.
Definition tools.hpp:29
@ kHandoff
Agent handoff tool.
Definition tools.hpp:32
@ kWebSearch
Built-in web search tool.
Definition tools.hpp:31
@ kFunction
User-defined function tool with C++ callback.
Definition tools.hpp:30
std::shared_ptr< FunctionTool > NewFunctionTool(std::string name, std::string description, const json &schema, const std::function< R(T)> &handler)
Create a FunctionTool with type-safe argument parsing.
Definition tools.hpp:173
InputItemType
Type discriminator for InputItem subclasses.
Definition input.hpp:44
@ kToolResult
Tool execution result (ToolResult).
Definition input.hpp:47
@ kToolCall
Tool call request from the model (ToolCallRequest).
Definition input.hpp:46
json NormalizeSchema(json schema)
Wraps a minimal properties-only JSON object into a full JSON Schema.
Top-level namespace for the ForestHub SDK.
JSON Schema normalization utilities.