NeoGraph 0.16.0
A C++20 Graph Agent Engine Library — LangGraph for C++
Loading...
Searching...
No Matches
agent.h
Go to the documentation of this file.
1
9#pragma once
10
11#include <neograph/api.h>
12#include <neograph/provider.h>
13#include <neograph/tool.h>
14#include <neograph/tool_dispatch.h> // ToolGate (issue #89)
15#include <functional>
16#include <memory>
17#include <string>
18#include <vector>
19
20namespace neograph { class RuntimeInterpositionController; }
21namespace neograph { class HookRuntime; }
22namespace neograph::llm {
23
39class NEOGRAPH_API Agent {
40 public:
48 Agent(std::shared_ptr<Provider> provider,
49 std::vector<std::unique_ptr<Tool>> tools,
50 const std::string& instructions = "",
51 const std::string& model = "");
52
53 // Move-only — `tools_` is `vector<unique_ptr<Tool>>`. Explicit
54 // declarations are required because Agent is NEOGRAPH_API and MSVC
55 // eagerly instantiates all special members for dll-exported classes
56 // (the implicit copy-assign tries to copy the vector → deleted →
57 // C2280). GCC/Clang only instantiate on use, so the bug was Windows-
58 // only and silently broke the wheel build.
59 Agent(const Agent&) = delete;
60 Agent& operator=(const Agent&) = delete;
61 Agent(Agent&&) noexcept = default;
62 Agent& operator=(Agent&&) noexcept = default;
63 ~Agent() = default;
64
75 sp::runtime::Result run(std::vector<sp::Message>& messages,
76 int max_iterations = 10);
80 sp::runtime::Result run(std::vector<sp::Message>& messages, int max_iterations,
81 ToolExecutionContext effect_context);
82
92 sp::runtime::Result run_stream(std::vector<sp::Message>& messages,
93 const std::function<void(const sp::Event&)>& on_event,
94 int max_iterations = 10);
95 sp::runtime::Result run_stream(std::vector<sp::Message>& messages,
96 const std::function<void(const sp::Event&)>& on_event, int max_iterations,
97 ToolExecutionContext effect_context);
98
104 sp::runtime::Result complete(const std::vector<sp::Message>& messages);
105
118 sp::Usage usage() const { return usage_->snapshot(); }
119 const std::vector<sp::runtime::Result>& outcomes() const noexcept { return outcomes_; }
120
134 void set_tool_gate(ToolGate gate) { tool_gate_ = std::move(gate); }
135
139 std::shared_ptr<ToolExecutionController> controller) {
140 tool_execution_controller_ = std::move(controller);
141 }
142
145 void set_tool_detection_timeout_seconds(int timeout_seconds) {
146 tool_detection_timeout_seconds_ = timeout_seconds;
147 }
148
150 void set_runtime_interposition(std::shared_ptr<::neograph::RuntimeInterpositionController> controller);
151
153 void set_hook_runtime(std::shared_ptr<::neograph::HookRuntime> runtime) {
154 hook_runtime_ = std::move(runtime);
155 }
156
157 private:
158 std::shared_ptr<Provider> provider_;
159 std::vector<std::unique_ptr<Tool>> tools_;
160 std::string instructions_;
161 std::string model_;
162
164 std::shared_ptr<UsageAccumulator> usage_ = std::make_shared<UsageAccumulator>();
165 std::vector<sp::runtime::Result> outcomes_;
166
168 ToolGate tool_gate_;
169
172 std::shared_ptr<ToolExecutionController> tool_execution_controller_;
173
174 int tool_detection_timeout_seconds_ = -1;
175 std::shared_ptr<::neograph::RuntimeInterpositionController> runtime_interposition_;
176 std::shared_ptr<::neograph::HookRuntime> hook_runtime_;
177
178 void ensure_system_message(std::vector<sp::Message>& messages);
179 sp::runtime::Result run_loop(std::vector<sp::Message>& messages, int max_iterations,
180 ToolExecutionContext execution, ProviderMode mode,
181 std::function<void(const sp::Event&)> on_event);
182 std::vector<ChatTool> get_tool_definitions() const;
183
186 std::vector<Tool*> tool_ptrs() const;
187};
188
189} // namespace neograph::llm
NEOGRAPH_API export/import macro for shared-library builds.
Standalone ReAct agent that loops between LLM calls and tool execution.
Definition agent.h:39
void set_tool_execution_controller(std::shared_ptr< ToolExecutionController > controller)
Inject a host-shared resource admission boundary for tool execution.
Definition agent.h:138
Agent(std::shared_ptr< Provider > provider, std::vector< std::unique_ptr< Tool > > tools, const std::string &instructions="", const std::string &model="")
Construct an agent with a provider and tools.
void set_tool_detection_timeout_seconds(int timeout_seconds)
Override the initial streamed tool-detection turn's absolute deadline.
Definition agent.h:145
void set_runtime_interposition(std::shared_ptr<::neograph::RuntimeInterpositionController > controller)
Opt into assembled, receipt-journaled provider dispatch for this agent.
void set_hook_runtime(std::shared_ptr<::neograph::HookRuntime > runtime)
Install the host-owned lifecycle boundary used by standalone tool dispatch.
Definition agent.h:153
void set_tool_gate(ToolGate gate)
Intercept every tool call before it runs (issue #89).
Definition agent.h:134
Per-invocation context for a cooperative asynchronous tool.
Definition tool.h:39
Abstract tool interface for callable functions.
The single implementation of "execute an assistant message's tool_calls".
std::function< asio::awaitable< ToolDecision >(ToolCall, ToolGateContext)> ToolGate
Called once per tool call, before any tool runs.