NeoGraph 0.16.0
A C++20 Graph Agent Engine Library — LangGraph for C++
Loading...
Searching...
No Matches
tool_set.h
1#pragma once
2
3#include <neograph/tool.h>
4
5#include <algorithm>
6#include <stdexcept>
7#include <memory>
8#include <utility>
9#include <vector>
10
11namespace neograph {
12
21class ToolSet {
22public:
23 ToolSet() = default;
24
25 explicit ToolSet(std::vector<std::unique_ptr<Tool>> tools) {
26 if (std::any_of(tools.begin(), tools.end(),
27 [](const auto& tool) { return !tool; }))
28 throw std::invalid_argument("ToolSet cannot own a null Tool");
29 if (!tools.empty())
30 unique_tools_ =
31 std::make_shared<const std::vector<std::unique_ptr<Tool>>>(
32 std::move(tools));
33 }
34
35 explicit ToolSet(std::vector<std::shared_ptr<Tool>> tools) {
36 if (std::any_of(tools.begin(), tools.end(),
37 [](const auto& tool) { return !tool; }))
38 throw std::invalid_argument("ToolSet cannot own a null Tool");
39 if (!tools.empty())
40 shared_tools_ =
41 std::make_shared<const std::vector<std::shared_ptr<Tool>>>(
42 std::move(tools));
43 }
44
45 std::vector<Tool*> view() const {
46 if (selection_) return *selection_;
47 std::vector<Tool*> tools;
48 tools.reserve(size());
49 if (unique_tools_)
50 for (const auto& tool : *unique_tools_) tools.push_back(tool.get());
51 if (shared_tools_)
52 for (const auto& tool : *shared_tools_) tools.push_back(tool.get());
53 return tools;
54 }
55
57 ToolSet select(std::vector<Tool*> tools) const {
58 for (Tool* tool : tools) {
59 const auto owns = [tool](const auto& owner) { return owner.get() == tool; };
60 const bool found = selection_
61 ? std::find(selection_->begin(), selection_->end(), tool) != selection_->end()
62 : (unique_tools_ &&
63 std::any_of(unique_tools_->begin(), unique_tools_->end(), owns))
64 || (shared_tools_ &&
65 std::any_of(shared_tools_->begin(), shared_tools_->end(), owns));
66 if (!found)
67 throw std::invalid_argument("ToolSet selection contains an unowned Tool");
68 }
69 ToolSet result;
70 if (!tools.empty()) {
71 result.unique_tools_ = unique_tools_;
72 result.shared_tools_ = shared_tools_;
73 result.selection_ = std::make_shared<const std::vector<Tool*>>(std::move(tools));
74 }
75 return result;
76 }
77
78 std::size_t size() const noexcept {
79 return selection_ ? selection_->size()
80 : (unique_tools_ ? unique_tools_->size() : 0)
81 + (shared_tools_ ? shared_tools_->size() : 0);
82 }
83 bool empty() const noexcept { return size() == 0; }
84
85private:
86 std::shared_ptr<const std::vector<std::unique_ptr<Tool>>> unique_tools_;
87 std::shared_ptr<const std::vector<std::shared_ptr<Tool>>> shared_tools_;
88 std::shared_ptr<const std::vector<Tool*>> selection_;
89};
90
91} // namespace neograph
Copyable owner of a fixed collection of tools.
Definition tool_set.h:21
ToolSet select(std::vector< Tool * > tools) const
Retain ownership while exposing only a node's authorized subset.
Definition tool_set.h:57
Abstract base class for tools that agents can call.
Definition tool.h:114
Abstract tool interface for callable functions.