NeoGraph 0.16.0
A C++20 Graph Agent Engine Library — LangGraph for C++
Loading...
Searching...
No Matches
server.h
Go to the documentation of this file.
1
44#pragma once
45
46#include <neograph/api.h>
47#include <neograph/a2a/types.h>
48#include <neograph/graph/execution.h>
49
50#ifdef NEOGRAPH_A2A_PROGRAM
53#endif
54
55#include <chrono>
56#include <functional>
57#include <cstddef>
58#include <memory>
59#include <optional>
60#include <string>
61#include <string_view>
62#include <vector>
63
64#ifdef NEOGRAPH_A2A_PROGRAM
65namespace neograph::program {
66class ProgramRuntime;
67class ProgramVersion;
68} // namespace neograph::program
69#endif
70
71namespace neograph::a2a {
72
73#ifdef NEOGRAPH_A2A_PROGRAM
74class ProgramAgentAdapter;
75#endif
76
77#ifdef NEOGRAPH_A2A_TESTING
78namespace test { class A2AServerTestAccess; }
79#endif
80
88class NEOGRAPH_API GraphAgentAdapter {
89 public:
90 virtual ~GraphAgentAdapter() = default;
91
93 virtual std::string input_channel() const { return "prompt"; }
94
96 virtual std::string output_channel() const { return "response"; }
97
101 virtual neograph::json build_initial_state(const std::string& user_text) const;
102
104 virtual std::optional<Artifact> build_output_artifact(
105 const neograph::json& output,
106 const std::string& task_id) const;
107
112 virtual neograph::json build_resume_value(const Message& message) const;
113};
114
116class NEOGRAPH_API StructuredOutputAdapter final : public GraphAgentAdapter {
117 public:
118 StructuredOutputAdapter(std::string contract,
119 int version,
120 std::string data_channel,
121 std::string input_channel = "prompt");
122
123 std::string input_channel() const override;
124 std::string output_channel() const override;
125 std::optional<Artifact> build_output_artifact(
126 const neograph::json& output,
127 const std::string& task_id) const override;
128
129 private:
130 std::string contract_;
131 int version_;
132 std::string data_channel_;
133 std::string input_channel_;
134};
135
148struct NEOGRAPH_API A2AHttpConfig {
155 std::vector<std::string> allowed_origins;
156
166 std::vector<std::string> allowed_hosts;
167
176 std::size_t max_payload_bytes = 4 * 1024 * 1024;
177};
178
194class NEOGRAPH_API A2AServer {
195 public:
199 A2AServer(std::shared_ptr<neograph::graph::GraphEngine> engine,
200 AgentCard card,
201 std::shared_ptr<GraphAgentAdapter> adapter = {});
204 AgentCard card,
205 std::shared_ptr<GraphAgentAdapter> adapter = {});
206
207#ifdef NEOGRAPH_A2A_PROGRAM
210
226 using CollaborationAuthenticator = std::function<std::optional<CollaborationPeerIdentity>(
227 std::string_view authorization_header)>;
228
229 A2AServer(std::shared_ptr<ProgramAgentAdapter> adapter,
230 AgentCard card,
231 CollaborationAuthenticator collaboration_authenticator = {},
232 bool require_authenticated_requests = false);
233
234 A2AServer(std::shared_ptr<neograph::program::ProgramRuntime> runtime,
235 neograph::program::ProgramVersion version,
236 std::string owner_scope,
237 AgentCard card,
238 std::shared_ptr<CollaborationMailbox> mailbox = {},
239 CollaborationAuthenticator collaboration_authenticator = {},
240 bool require_authenticated_requests = false);
241 A2AServer(std::shared_ptr<neograph::program::ProgramRuntime> runtime,
242 neograph::program::ProgramVersion version,
243 AgentCard card,
244 std::string owner_scope,
245 std::shared_ptr<CollaborationMailbox> mailbox = {},
246 CollaborationAuthenticator collaboration_authenticator = {},
247 bool require_authenticated_requests = false);
248#endif
249
250 ~A2AServer();
251
256 bool start(const std::string& host, int port);
257
261 bool start_async(const std::string& host, int port);
262
268 void stop();
269
271 bool is_running() const;
272
274 int port() const;
275
288
298 void set_trust_forwarded_headers(bool enabled);
299
300 private:
301#ifdef NEOGRAPH_A2A_TESTING
302 friend class test::A2AServerTestAccess;
303#endif
304 struct Impl;
305 std::unique_ptr<Impl> impl_;
306};
307
308} // namespace neograph::a2a
309
310#ifdef NEOGRAPH_A2A_TESTING
311namespace neograph::a2a::test {
312
313class A2AServerTestAccess {
314 public:
315 static void set_max_inflight_runs(A2AServer& server, std::size_t cap);
317 static void set_stream_keepalive(A2AServer& server, std::chrono::milliseconds interval);
320 static void set_stream_handoff_hook(A2AServer& server, std::function<void()> hook);
322 static bool draining(const A2AServer& server);
323};
324
325} // namespace neograph::a2a::test
326#endif
Core data types for the Agent-to-Agent (A2A) protocol.
NEOGRAPH_API export/import macro for shared-library builds.
HTTP server exposing a NeoGraph as an A2A agent.
Definition server.h:194
bool start(const std::string &host, int port)
Bind + listen.
void stop()
Signal the server to stop accepting connections and join.
A2AServer(neograph::graph::GraphExecution execution, AgentCard card, std::shared_ptr< GraphAgentAdapter > adapter={})
Preferred constructor: retain only execution-facing capabilities.
A2AServer(std::shared_ptr< neograph::graph::GraphEngine > engine, AgentCard card, std::shared_ptr< GraphAgentAdapter > adapter={})
bool is_running() const
True once start_async() has the server listening.
void set_http_config(A2AHttpConfig config)
Set the Host and Origin checks and the request size limit (see A2AHttpConfig).
void set_trust_forwarded_headers(bool enabled)
When the card has no configured url, the served card advertises the address the client used,...
bool start_async(const std::string &host, int port)
Bind + spawn a worker thread; returns when the server is ready to accept connections.
int port() const
Bound port — useful when caller passed 0 to bind a free port.
Adapt a NeoGraph run to the A2A request/response shape.
Definition server.h:88
virtual neograph::json build_resume_value(const Message &message) const
The value a follow-up message to an INPUT_REQUIRED task resumes the interrupted run with (spec 3....
virtual std::string output_channel() const
Channel name to read the agent's text response from (default "response").
Definition server.h:96
virtual neograph::json build_initial_state(const std::string &user_text) const
Hook to populate the initial graph state.
virtual std::string input_channel() const
Channel name to write the inbound user text into (default "prompt").
Definition server.h:93
virtual std::optional< Artifact > build_output_artifact(const neograph::json &output, const std::string &task_id) const
Optionally promote a graph output into a typed A2A artifact.
Adapt one JSON-valued graph output channel into a versioned A2A DataPart.
Definition server.h:116
std::string input_channel() const override
Channel name to write the inbound user text into (default "prompt").
std::string output_channel() const override
Channel name to read the agent's text response from (default "response").
std::optional< Artifact > build_output_artifact(const neograph::json &output, const std::string &task_id) const override
Optionally promote a graph output into a typed A2A artifact.
Owning, concrete execution capability for protocol hosts.
Definition execution.h:18
Program-owned collaboration envelopes carried by A2A.
ProgramRuntime-backed A2A collaboration adapter.
Which Host and Origin values an A2AServer answers, and how large a request body it reads.
Definition server.h:148
std::vector< std::string > allowed_hosts
Host header names the server answers: host names, IPv4 addresses and bracketed IPv6 addresses ([::1])...
Definition server.h:166
std::vector< std::string > allowed_origins
Exact allowed Origin values.
Definition server.h:155
Subset of AgentCard required to interact (spec §5.5).
Definition types.h:222
A single conversational turn (spec §6.4).
Definition types.h:89