NeoGraph 0.16.0
A C++20 Graph Agent Engine Library — LangGraph for C++
Loading...
Searching...
No Matches
sse_parser.h
Go to the documentation of this file.
1
23#pragma once
24
25#include <neograph/api.h>
26
27#include <cstddef>
28#include <cstdint>
29#include <optional>
30#include <string>
31#include <string_view>
32#include <vector>
33
34namespace neograph::async {
35
36struct SseEvent {
37 std::string event;
38 std::string data;
39 std::string id;
40};
41
42struct SseParserOptions {
43 std::size_t max_partial_line_bytes = 64u * 1024u;
44 std::size_t max_event_bytes = 1u * 1024u * 1024u;
45 std::size_t max_pending_events = 1024u;
46 std::size_t max_pending_bytes = 8u * 1024u * 1024u;
47};
48
49class NEOGRAPH_API SseEventParser {
50public:
52 explicit SseEventParser(SseParserOptions options = {});
53
58 void feed(std::string_view bytes);
59
62 std::vector<SseEvent> drain();
63
65 const std::string& last_event_id() const noexcept { return last_id_; }
66
68 std::optional<std::uint64_t> retry_ms() const noexcept { return retry_ms_; }
69
71 void reset() noexcept;
72
73private:
74 void consume_line(std::string_view line);
75 void finish_event();
76 void ensure_can_add(std::size_t current, std::size_t addition,
77 std::size_t limit, const char* message);
78 [[noreturn]] void fail_limit(const char* message);
79
80 SseParserOptions options_;
81 std::string raw_;
82 std::string cur_data_;
83 std::string cur_event_;
84 std::string last_id_;
85 std::optional<std::uint64_t> retry_ms_;
86 std::size_t cur_event_bytes_ = 0;
87 bool cur_has_data_ = false;
88 bool skip_lf_ = false;
89 bool at_start_ = true;
90 std::vector<SseEvent> pending_;
91 std::size_t pending_bytes_ = 0;
92 bool failed_ = false;
93};
94
95} // namespace neograph::async
NEOGRAPH_API export/import macro for shared-library builds.