NeoGraph 0.16.0
A C++20 Graph Agent Engine Library — LangGraph for C++
Loading...
Searching...
No Matches
client.h
Go to the documentation of this file.
1
21#pragma once
22
23#include <neograph/api.h>
24#include <neograph/a2a/types.h>
25
26#include <asio/awaitable.hpp>
27
28#include <chrono>
29#include <functional>
30#include <memory>
31#include <atomic>
32#include <mutex>
33#include <optional>
34#include <stdexcept>
35#include <string>
36
37namespace neograph::graph { class CancelToken; }
38
39namespace neograph::a2a {
40
41#ifdef NEOGRAPH_A2A_TESTING
42namespace test { class A2AClientTestAccess; }
43#endif
44
47class NEOGRAPH_API A2ARpcError : public std::runtime_error {
48 public:
49 A2ARpcError(int code, const std::string& what)
50 : std::runtime_error(what), code_(code) {}
51 int code() const noexcept { return code_; }
52
53 private:
54 int code_;
55};
56
99class NEOGRAPH_API A2AClient {
100 public:
103 explicit A2AClient(std::string base_url);
104
112 void set_timeout(std::chrono::seconds t);
113
120 void set_connect_timeout(std::chrono::seconds t);
121
126
129 std::optional<WireDialect> wire_dialect() const;
130
136 void set_authorization_header(std::string authorization_header);
137
144 AgentCard fetch_agent_card(bool force = false);
145
147 asio::awaitable<AgentCard> fetch_agent_card_async(bool force = false);
148
161 const std::string& text,
162 const std::string& task_id = "",
163 const std::string& context_id = "",
164 graph::CancelToken* cancel = nullptr);
165
169 graph::CancelToken* cancel = nullptr);
170
174 asio::awaitable<Task> send_message_async(const MessageSendParams& params);
175
177 Task get_task(const std::string& task_id, int history_length = 0);
178 asio::awaitable<Task> get_task_async(const std::string& task_id, int history_length = 0);
179
181 Task cancel_task(const std::string& task_id);
182 asio::awaitable<Task> cancel_task_async(const std::string& task_id);
183
196 using EventCallback = std::function<bool(const StreamEvent&)>;
197
198 Task send_message_stream(const std::string& text,
199 EventCallback on_event,
200 const std::string& task_id = "",
201 const std::string& context_id = "");
202
203 Task send_message_stream(const MessageSendParams& params,
204 EventCallback on_event);
205
209 json rpc_call(const std::string& method, const json& params,
210 std::optional<WireDialect> dialect = std::nullopt);
211 asio::awaitable<json> rpc_call_async(
212 const std::string& method, const json& params,
213 std::optional<WireDialect> dialect = std::nullopt);
214
215 const std::string& base_url() const { return base_url_; }
216
219 std::string endpoint_url() const;
220
221 private:
222#ifdef NEOGRAPH_A2A_TESTING
223 friend class test::A2AClientTestAccess;
224#endif
225 std::string base_url_;
226 std::chrono::seconds timeout_ = std::chrono::seconds(0); // 0 = no whole-exchange bound
227 std::chrono::seconds connect_timeout_ = std::chrono::seconds(30); // 0 = no connect bound
228 std::atomic<int> request_id_{0};
229
230 mutable std::shared_ptr<std::mutex> state_mutex_ = std::make_shared<std::mutex>();
231 bool card_loading_ = false;
232
234 AgentCard cached_card_;
235 bool card_loaded_ = false;
237 bool card_unavailable_ = false;
238 bool allow_cross_origin_ = false;
239 std::string authorization_header_;
240
242 struct Selection {
243 WireDialect dialect = WireDialect::V0_3;
244 std::string tenant;
245 std::string url;
246 };
247 std::optional<Selection> selection_;
248
252 std::optional<Selection> resolve_selection();
254 asio::awaitable<std::optional<Selection>> select_async();
255 void remember_probe(WireDialect dialect);
256
257 asio::awaitable<json> post_rpc(const std::string& url, const std::string& method,
258 const json& params, std::optional<WireDialect> dialect);
259
263 asio::awaitable<json> call_method(
264 const char* v1_method, const char* v03_method,
265 const std::function<json(WireDialect, const std::string& tenant)>& build);
266
267 Task stream_once(WireDialect dialect, const std::string& tenant, const std::string& url,
268 const MessageSendParams& params, const EventCallback& on_event,
269 bool& saw_events);
270
271 std::string request_authorization_header() const;
272
273 std::chrono::seconds request_timeout() const;
274 std::chrono::seconds request_connect_timeout() const;
275};
276
277} // namespace neograph::a2a
Core data types for the Agent-to-Agent (A2A) protocol.
WireDialect
A2A wire dialect — the JSON encoding of the data model.
Definition types.h:40
NEOGRAPH_API export/import macro for shared-library builds.
A2A client — call a remote agent over JSON-RPC + HTTP.
Definition client.h:99
AgentCard fetch_agent_card(bool force=false)
GET /.well-known/agent-card.json.
void set_allow_cross_origin_interfaces(bool allow)
Allow the AgentCard to direct RPCs to an interface URL on another origin than base_url (default false...
asio::awaitable< AgentCard > fetch_agent_card_async(bool force=false)
Async variant of fetch_agent_card.
Task get_task(const std::string &task_id, int history_length=0)
tasks/get — fetch the latest snapshot of a task.
json rpc_call(const std::string &method, const json &params, std::optional< WireDialect > dialect=std::nullopt)
Lower-level: arbitrary JSON-RPC method, sent verbatim.
Task send_message_sync(const std::string &text, const std::string &task_id="", const std::string &context_id="", graph::CancelToken *cancel=nullptr)
message/send — convenience wrapper around send_message_sync().
Task cancel_task(const std::string &task_id)
tasks/cancel — request cancellation. Returns updated Task.
void set_timeout(std::chrono::seconds t)
Bound the whole exchange of each request (connect, write, and reading the reply or the event stream t...
void set_authorization_header(std::string authorization_header)
Set the explicit Authorization header for subsequent RPC requests.
std::optional< WireDialect > wire_dialect() const
Wire dialect in use: selected from the fetched AgentCard or learned by probing.
std::function< bool(const StreamEvent &)> EventCallback
SendStreamingMessage / message/stream — send a message and receive SSE-framed status updates as the a...
Definition client.h:196
Task send_message_sync(const MessageSendParams &params, graph::CancelToken *cancel=nullptr)
Send an arbitrary Message (multipart, file, structured data).
void set_connect_timeout(std::chrono::seconds t)
Bound the time to resolve the host, connect and finish the TLS handshake of each request to t (defaul...
std::string endpoint_url() const
The JSON-RPC endpoint in use: the selected interface URL, or base_url before a selection or when the ...
asio::awaitable< Task > send_message_async(const MessageSendParams &params)
Async variant — server's response payload may be either a Message or a Task; we coerce both into a Ta...
A2AClient(std::string base_url)
JSON-RPC error object returned by the remote agent.
Definition client.h:47
Cooperative cancel primitive shared between caller and engine.
Definition cancel.h:82
Subset of AgentCard required to interact (spec §5.5).
Definition types.h:222
Params object for message/send and message/stream.
Definition types.h:137
Tagged union over the two streaming event types + the terminal Task message.
Definition types.h:171
A unit of work tracked by the agent (spec §6.1).
Definition types.h:118