NeuralPlus 0.2.0
A small C++17 SDK for AI clients, sessions, tools, and tracing
Loading...
Searching...
No Matches
tool.hpp
Go to the documentation of this file.
1// Copyright 2026 Aniket Kulkarni
2// SPDX-License-Identifier: Apache-2.0
3
6
7#pragma once
8
11#include "neuralplus/types.hpp"
12
13#include <functional>
14#include <memory>
15#include <mutex>
16#include <string>
17#include <utility>
18#include <vector>
19
20namespace neuralplus {
21
22class AIClient;
23
28class NEURALPLUS_API ToolContext final {
29public:
31 ToolContext(std::string session_id,
32 std::string run_id,
33 std::string call_id,
34 SessionState& state);
35
37 [[nodiscard]] const std::string& session_id() const noexcept;
38
40 [[nodiscard]] const std::string& run_id() const noexcept;
41
43 [[nodiscard]] const std::string& call_id() const noexcept;
44
46 [[nodiscard]] SessionState& state() const noexcept;
47
48private:
49 ToolContext(const ToolContext&) = delete;
50 ToolContext& operator=(const ToolContext&) = delete;
51
52 std::string session_id_;
53 std::string run_id_;
54 std::string call_id_;
55 SessionState* state_;
56};
57
59struct NEURALPLUS_API ToolOutput {
61 std::vector<Content> contents;
62
64 bool is_error{false};
65
67 [[nodiscard]] static ToolOutput text(std::string value);
68
70 [[nodiscard]] static ToolOutput json(JsonValue value);
71
73 [[nodiscard]] static ToolOutput error(std::string value);
74};
75
81class NEURALPLUS_API Tool {
82public:
83 virtual ~Tool();
84
86 [[nodiscard]] virtual const ToolSpec& spec() const noexcept = 0;
87
89 virtual ToolOutput invoke(ToolContext& context,
90 const JsonValue& arguments) = 0;
91
92protected:
93 Tool() = default;
94
95private:
96 friend class AIClient;
97
98 Tool(const Tool&) = delete;
99 Tool& operator=(const Tool&) = delete;
100
101 mutable std::recursive_mutex invocation_mutex_;
102};
103
105class NEURALPLUS_API FunctionTool final : public Tool {
106public:
108 using Function = std::function<ToolOutput(ToolContext&, const JsonValue&)>;
109
112 ~FunctionTool() override;
113
115 [[nodiscard]] const ToolSpec& spec() const noexcept override;
116
118 ToolOutput invoke(ToolContext& context,
119 const JsonValue& arguments) override;
120
121private:
122 FunctionTool(const FunctionTool&) = delete;
123 FunctionTool& operator=(const FunctionTool&) = delete;
124
125 ToolSpec spec_;
126 Function function_;
127};
128
130using Tools = std::vector<std::shared_ptr<Tool>>;
131
132} // namespace neuralplus
Definition client.hpp:61
Callback-backed Tool for applications that do not need a subclass.
Definition tool.hpp:105
std::function< ToolOutput(ToolContext &, const JsonValue &)> Function
Callback signature for one tool invocation.
Definition tool.hpp:108
const ToolSpec & spec() const noexcept override
Returns the immutable declaration supplied at construction.
FunctionTool(ToolSpec spec, Function function)
Creates a tool from its declaration and callback.
Definition session.hpp:30
Definition tool.hpp:28
const std::string & session_id() const noexcept
Returns the session identifier associated with this invocation.
ToolContext(std::string session_id, std::string run_id, std::string call_id, SessionState &state)
Creates the context for one correlated tool invocation.
Definition tool.hpp:81
virtual const ToolSpec & spec() const noexcept=0
Returns the immutable declaration advertised to providers.
Normalized multimodal result from a tool.
Definition tool.hpp:59
static ToolOutput text(std::string value)
Creates a successful text result.
static ToolOutput error(std::string value)
Creates an unsuccessful text result.
std::vector< Content > contents
Text, media, file, or provider-extension parts returned by the tool.
Definition tool.hpp:61
static ToolOutput json(JsonValue value)
Creates a successful JSON result serialized as text content.
Description and JSON Schema advertised to a model.
Definition types.hpp:189
std::vector< std::shared_ptr< Tool > > Tools
Shared tool collection accepted by ClientOptions.
Definition tool.hpp:130
nlohmann::json JsonValue
Definition types.hpp:26