NeoGraph 0.10.0
A C++17 Graph Agent Engine Library — LangGraph for C++
Loading...
Searching...
No Matches
state.h
Go to the documentation of this file.
1
9#pragma once
10
11#include <neograph/api.h>
13#include <mutex>
14#include <shared_mutex>
15#include <vector>
16
17namespace neograph::graph {
18
26class NEOGRAPH_API GraphState {
27public:
35 void init_channel(const std::string& name,
36 ReducerType type,
37 ReducerFn reducer,
38 const json& initial_value = json());
39
45 json get(const std::string& channel) const;
46
51 std::vector<ChatMessage> get_messages() const;
52
58 void write(const std::string& channel, const json& value);
59
68 void apply_writes(const std::vector<ChannelWrite>& writes);
69
75 uint64_t channel_version(const std::string& channel) const;
76
81 uint64_t global_version() const;
82
87 json serialize() const;
88
93 void restore(const json& data);
94
99 std::vector<std::string> channel_names() const;
100
101 // v1.0 (9d): the v0.3 `run_cancel_token_` smuggling channel is gone.
102 // Cancel propagation now flows exclusively through
103 // `RunContext::cancel_token` on `NodeInput::ctx` (engine threads it
104 // through every dispatch; multi-Send fan-out copies the context by
105 // value, so the cancel token survives the isolated-state boundary
106 // without a serialize/restore hop).
107
108private:
109 std::map<std::string, Channel> channels_;
110 uint64_t global_version_ = 0;
111 mutable std::shared_mutex mutex_;
112};
113
114} // namespace neograph::graph
NEOGRAPH_API export/import macro for shared-library builds.
Thread-safe container for all graph state channels.
Definition state.h:26
uint64_t channel_version(const std::string &channel) const
Get the version counter of a specific channel.
std::vector< ChatMessage > get_messages() const
Convenience method to read the "messages" channel as a vector of ChatMessage.
void write(const std::string &channel, const json &value)
Write a value to a single channel through its reducer (exclusive lock).
std::vector< std::string > channel_names() const
List all channel names in this state.
void restore(const json &data)
Restore state from a JSON snapshot (for time-travel).
void apply_writes(const std::vector< ChannelWrite > &writes)
Apply a batch of channel writes atomically (exclusive lock).
uint64_t global_version() const
Get the global version counter across all channels.
json serialize() const
Serialize the entire state to JSON (for checkpointing).
json get(const std::string &channel) const
Read a channel's current value (thread-safe, shared lock).
void init_channel(const std::string &name, ReducerType type, ReducerFn reducer, const json &initial_value=json())
Initialize a new channel with a reducer and optional initial value.
Graph engine type definitions: channels, edges, nodes, events, and control flow.
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