NeoGraph 0.10.0
A C++17 Graph Agent Engine Library — LangGraph for C++
Loading...
Searching...
No Matches
types.h
Go to the documentation of this file.
1
9#pragma once
10
11#include <neograph/api.h>
12#include <neograph/types.h>
13#include <neograph/provider.h>
14#include <neograph/tool.h>
15#include <functional>
16#include <map>
17#include <memory>
18#include <optional>
19#include <stdexcept>
20#include <string>
21#include <vector>
22#include <cstdint>
23
24// Windows SDK (pulled in transitively via asio on Win32) defines a
25// bunch of ALL-CAPS macros — notably ERROR (wingdi.h) and DEBUG —
26// that silently clobber enumerator names. `enum class Type { ERROR }`
27// below becomes `enum class Type { 0 }` and every downstream parse
28// collapses. Scrub the ones we actually use before declaring the
29// enums. No-op outside of Windows.
30#ifdef _WIN32
31# ifdef ERROR
32# undef ERROR
33# endif
34# ifdef DEBUG
35# undef DEBUG
36# endif
37# ifdef IN
38# undef IN
39# endif
40# ifdef OUT
41# undef OUT
42# endif
43#endif
44
45namespace neograph::graph {
46
47// Forward declaration
48class GraphState;
49
53enum class ReducerType {
54 OVERWRITE,
55 APPEND,
56 CUSTOM
57};
58
65using ReducerFn = std::function<json(const json& current, const json& incoming)>;
66
74struct Channel {
75 std::string name;
76 ReducerType reducer_type = ReducerType::OVERWRITE;
78 json value;
79 uint64_t version = 0;
80};
81
86 std::string channel;
87 json value;
88};
89
101class NEOGRAPH_API NodeInterrupt : public std::runtime_error {
102public:
105 explicit NodeInterrupt(const std::string& reason)
106 : std::runtime_error(reason), reason_(reason) {}
107
110 const std::string& reason() const { return reason_; }
111private:
112 std::string reason_;
113};
114
144struct Send {
145 std::string target_node;
146 json input;
147};
148
160struct Command {
161 std::string goto_node;
162 std::vector<ChannelWrite> updates;
163};
164
173 int max_retries = 0;
175 float backoff_multiplier = 2.0f;
176 int max_delay_ms = 5000;
183 float jitter_pct = 0.0f;
184};
185
194enum class StreamMode : uint8_t {
195 EVENTS = 0x01,
196 TOKENS = 0x02,
197 VALUES = 0x04,
198 UPDATES = 0x08,
199 DEBUG = 0x10,
200 ALL = 0xFF
201};
202
208 return static_cast<StreamMode>(static_cast<uint8_t>(a) | static_cast<uint8_t>(b));
209}
215 return static_cast<StreamMode>(static_cast<uint8_t>(a) & static_cast<uint8_t>(b));
216}
221inline bool has_mode(StreamMode flags, StreamMode test) {
222 return static_cast<uint8_t>(flags & test) != 0;
223}
224
228struct Edge {
229 std::string from;
230 std::string to;
231};
232
240 std::string from;
241 std::string condition;
242 std::map<std::string, std::string> routes;
243};
244
252 std::shared_ptr<Provider> provider;
261 std::vector<Tool*> tools;
262 std::string model;
263 std::string instructions;
265};
266
272 enum class Type {
273 NODE_START,
274 NODE_END,
275 LLM_TOKEN,
277 INTERRUPT,
278 ERROR
279 };
281 std::string node_name;
282 json data;
283};
284
287using GraphStreamCallback = std::function<void(const GraphEvent&)>;
288
298 std::vector<ChannelWrite> writes;
299 std::optional<Command> command;
300 std::vector<Send> sends;
301
302 NodeResult() = default;
303
306 NodeResult(std::vector<ChannelWrite> w) : writes(std::move(w)) {}
307};
308
314using ConditionFn = std::function<std::string(const GraphState&)>;
315
317constexpr const char* START_NODE = "__start__";
319constexpr const char* END_NODE = "__end__";
320
321} // namespace neograph::graph
NEOGRAPH_API export/import macro for shared-library builds.
Thread-safe container for all graph state channels.
Definition state.h:26
Exception thrown from inside a node to trigger a dynamic breakpoint.
Definition types.h:101
const std::string & reason() const
Get the interrupt reason.
Definition types.h:110
NodeInterrupt(const std::string &reason)
Construct a NodeInterrupt with a reason message.
Definition types.h:105
std::function< void(const GraphEvent &)> GraphStreamCallback
Callback for receiving graph execution events.
Definition types.h:287
constexpr const char * START_NODE
Special node name representing the graph entry point.
Definition types.h:317
StreamMode operator|(StreamMode a, StreamMode b)
Bitwise OR for StreamMode flags.
Definition types.h:207
bool has_mode(StreamMode flags, StreamMode test)
Check if a specific stream mode flag is set.
Definition types.h:221
constexpr const char * END_NODE
Special node name representing the graph exit point.
Definition types.h:319
StreamMode
Bitfield flags for selecting which events to stream during execution.
Definition types.h:194
@ TOKENS
LLM_TOKEN events (streaming tokens).
@ UPDATES
Channel write deltas per node.
@ EVENTS
NODE_START, NODE_END, INTERRUPT, ERROR events.
@ VALUES
Full state snapshot after each super-step.
@ DEBUG
Internal debug info (retry attempts, routing decisions).
StreamMode operator&(StreamMode a, StreamMode b)
Bitwise AND for StreamMode flags.
Definition types.h:214
std::function< json(const json &current, const json &incoming)> ReducerFn
Custom reducer function signature.
Definition types.h:65
ReducerType
Strategy for merging values when writing to a channel.
Definition types.h:53
@ OVERWRITE
Replace the current value entirely.
@ APPEND
Append to the current value (arrays are concatenated).
@ CUSTOM
Use a custom reducer function.
std::function< std::string(const GraphState &)> ConditionFn
Condition function signature for conditional edge routing.
Definition types.h:314
Abstract LLM provider interface.
Output of a node: a write to a named channel.
Definition types.h:85
json value
Value to write through the channel's reducer.
Definition types.h:87
std::string channel
Target channel name.
Definition types.h:86
A state channel that holds a value with a reducer and version tracking.
Definition types.h:74
ReducerType reducer_type
Merge strategy.
Definition types.h:76
uint64_t version
Version counter (incremented on each write).
Definition types.h:79
json value
Current channel value.
Definition types.h:78
ReducerFn reducer
Custom reducer (when type == CUSTOM).
Definition types.h:77
std::string name
Channel name.
Definition types.h:75
Combined routing override + state update.
Definition types.h:160
std::vector< ChannelWrite > updates
State updates to apply before routing.
Definition types.h:162
std::string goto_node
Next node to execute (overrides edge routing).
Definition types.h:161
A conditional edge with runtime routing based on state.
Definition types.h:239
std::string from
Source node name.
Definition types.h:240
std::string condition
Condition function name in ConditionRegistry.
Definition types.h:241
std::map< std::string, std::string > routes
Mapping of condition result to destination node.
Definition types.h:242
A static edge connecting two nodes.
Definition types.h:228
std::string to
Destination node name.
Definition types.h:230
std::string from
Source node name.
Definition types.h:229
An event emitted during graph execution for streaming.
Definition types.h:270
json data
Event-specific data payload.
Definition types.h:282
std::string node_name
Name of the node that generated this event.
Definition types.h:281
Type type
The event type.
Definition types.h:280
Type
Event type enumeration.
Definition types.h:272
@ NODE_START
A node began execution.
@ LLM_TOKEN
A token was received from the LLM.
@ CHANNEL_WRITE
A value was written to a channel.
@ INTERRUPT
Execution was interrupted (HITL).
@ ERROR
An error occurred during execution.
@ NODE_END
A node finished execution.
Dependency injection context passed to nodes during construction.
Definition types.h:251
std::shared_ptr< Provider > provider
LLM provider for making completions.
Definition types.h:252
json extra_config
Additional configuration (node-type specific).
Definition types.h:264
std::string model
Model name override (empty = use provider default).
Definition types.h:262
std::vector< Tool * > tools
Non-owning tool pointers consumed by node factories at compile() time.
Definition types.h:261
std::string instructions
System prompt / instructions for the LLM.
Definition types.h:263
Extended result returned by node execution.
Definition types.h:297
std::vector< Send > sends
If non-empty, triggers dynamic fan-out.
Definition types.h:300
std::optional< Command > command
If set, overrides edge-based routing.
Definition types.h:299
std::vector< ChannelWrite > writes
Channel writes from this node.
Definition types.h:298
NodeResult(std::vector< ChannelWrite > w)
Construct from plain channel writes (backward compatible).
Definition types.h:306
Retry policy for node execution with exponential backoff.
Definition types.h:172
int initial_delay_ms
Delay before the first retry (milliseconds).
Definition types.h:174
int max_delay_ms
Maximum delay cap (milliseconds).
Definition types.h:176
float jitter_pct
Random jitter as a fraction of the computed delay, in [0, 1].
Definition types.h:183
int max_retries
Maximum retry attempts. 0 = no retry.
Definition types.h:173
float backoff_multiplier
Multiplier applied to delay after each retry.
Definition types.h:175
Dynamic fan-out request (map-reduce pattern).
Definition types.h:144
json input
Channel writes for that invocation.
Definition types.h:146
std::string target_node
Node to dispatch.
Definition types.h:145
Abstract tool interface for callable functions.
Foundation types for NeoGraph: messages, tool calls, and LLM completions.