-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAiSseJsonAssembler.cpp
More file actions
723 lines (631 loc) · 23.9 KB
/
AiSseJsonAssembler.cpp
File metadata and controls
723 lines (631 loc) · 23.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
#include <algorithm>
#include <cctype>
#include <cstdint>
#include <deque>
#include <limits>
#include <optional>
#include <stdexcept>
#include <string>
#include <string_view>
#include <utility>
#include <vector>
namespace vibe::streaming {
enum class JsonValueKind {
kObject,
kArray,
kString,
kNumber,
kBoolean,
kNull
};
struct CompletedJsonValue {
std::string json;
JsonValueKind kind = JsonValueKind::kObject;
bool from_sse = false;
};
struct AssemblerStats {
std::uint64_t completed_values = 0;
std::uint64_t discarded_noise_bytes = 0;
std::uint64_t discarded_invalid_candidates = 0;
std::uint64_t sse_events_seen = 0;
std::uint64_t sse_done_markers = 0;
std::size_t max_depth_observed = 0;
std::size_t buffered_json_bytes = 0;
std::size_t buffered_transport_bytes = 0;
};
struct AssemblerLimits {
std::size_t max_buffered_transport_bytes = 256 * 1024;
std::size_t max_buffered_json_bytes = 2 * 1024 * 1024;
std::size_t max_json_depth = 128;
std::size_t max_single_json_bytes = 1 * 1024 * 1024;
std::size_t max_completed_queue = 256;
bool accept_top_level_primitives = true;
};
class AssemblerError final : public std::runtime_error {
public:
using std::runtime_error::runtime_error;
};
namespace detail {
inline bool IsWhitespace(char ch) {
switch (ch) {
case ' ':
case '\n':
case '\r':
case '\t':
return true;
default:
return false;
}
}
inline std::size_t SkipWhitespace(std::string_view input, std::size_t index) {
while (index < input.size() && IsWhitespace(input[index])) {
++index;
}
return index;
}
inline std::string_view Trim(std::string_view input) {
const std::size_t start = SkipWhitespace(input, 0);
std::size_t end = input.size();
while (end > start && IsWhitespace(input[end - 1])) {
--end;
}
return input.substr(start, end - start);
}
inline std::string_view TrimLeft(std::string_view input) {
return input.substr(SkipWhitespace(input, 0));
}
class Utf8Validator {
public:
void Consume(std::string_view chunk) {
for (const unsigned char byte : chunk) {
ConsumeByte(byte);
}
}
bool IsBoundary() const {
return expected_continuations_ == 0;
}
private:
void ConsumeByte(unsigned char byte) {
if (expected_continuations_ == 0) {
if ((byte & 0x80u) == 0) {
return;
}
if ((byte & 0xE0u) == 0xC0u) {
expected_continuations_ = 1;
if (byte < 0xC2u) {
throw AssemblerError("invalid UTF-8 leading byte");
}
return;
}
if ((byte & 0xF0u) == 0xE0u) {
expected_continuations_ = 2;
return;
}
if ((byte & 0xF8u) == 0xF0u) {
if (byte > 0xF4u) {
throw AssemblerError("invalid UTF-8 leading byte");
}
expected_continuations_ = 3;
return;
}
throw AssemblerError("invalid UTF-8 leading byte");
}
if ((byte & 0xC0u) != 0x80u) {
throw AssemblerError("invalid UTF-8 continuation byte");
}
--expected_continuations_;
}
int expected_continuations_ = 0;
};
enum class ParseStatus {
kComplete,
kIncomplete,
kError
};
struct ParseResult {
ParseStatus status = ParseStatus::kError;
std::size_t next = 0;
JsonValueKind kind = JsonValueKind::kObject;
std::size_t max_depth_observed = 0;
std::string error;
};
class JsonParser {
public:
JsonParser(std::string_view input, const AssemblerLimits& limits)
: input_(input), limits_(limits) {}
ParseResult ParseSingleValue(std::size_t start) {
const std::size_t begin = SkipWhitespace(input_, start);
if (begin >= input_.size()) {
return {.status = ParseStatus::kIncomplete, .next = begin};
}
std::size_t cursor = begin;
const ParseResult value = ParseValue(cursor, 0);
if (value.status != ParseStatus::kComplete) {
return value;
}
cursor = SkipWhitespace(input_, cursor);
return {
.status = ParseStatus::kComplete,
.next = cursor,
.kind = value.kind,
.max_depth_observed = max_depth_observed_
};
}
private:
ParseResult ParseValue(std::size_t& index, std::size_t depth) {
if (depth > limits_.max_json_depth) {
return Error("JSON depth limit exceeded", index);
}
max_depth_observed_ = std::max(max_depth_observed_, depth);
if (index >= input_.size()) {
return {.status = ParseStatus::kIncomplete, .next = index};
}
const char ch = input_[index];
if (ch == '{') {
return ParseObject(index, depth + 1);
}
if (ch == '[') {
return ParseArray(index, depth + 1);
}
if (ch == '"') {
return ParseStringValue(index);
}
if (ch == '-' || (ch >= '0' && ch <= '9')) {
return ParseNumber(index);
}
if (ch == 't' || ch == 'f') {
return ParseBoolean(index);
}
if (ch == 'n') {
return ParseNull(index);
}
return Error("unexpected JSON token", index);
}
ParseResult ParseObject(std::size_t& index, std::size_t depth) {
++index;
index = SkipWhitespace(input_, index);
if (index >= input_.size()) {
return {.status = ParseStatus::kIncomplete, .next = index};
}
if (input_[index] == '}') {
++index;
return {.status = ParseStatus::kComplete, .next = index, .kind = JsonValueKind::kObject};
}
for (;;) {
ParseResult key = ParseStringValue(index);
if (key.status != ParseStatus::kComplete) {
return key;
}
index = SkipWhitespace(input_, index);
if (index >= input_.size()) {
return {.status = ParseStatus::kIncomplete, .next = index};
}
if (input_[index] != ':') {
return Error("expected ':' after object key", index);
}
++index;
index = SkipWhitespace(input_, index);
ParseResult value = ParseValue(index, depth);
if (value.status != ParseStatus::kComplete) {
return value;
}
index = SkipWhitespace(input_, index);
if (index >= input_.size()) {
return {.status = ParseStatus::kIncomplete, .next = index};
}
const char ch = input_[index];
if (ch == '}') {
++index;
return {.status = ParseStatus::kComplete, .next = index, .kind = JsonValueKind::kObject};
}
if (ch != ',') {
return Error("expected ',' or '}' inside object", index);
}
++index;
index = SkipWhitespace(input_, index);
if (index >= input_.size()) {
return {.status = ParseStatus::kIncomplete, .next = index};
}
}
}
ParseResult ParseArray(std::size_t& index, std::size_t depth) {
++index;
index = SkipWhitespace(input_, index);
if (index >= input_.size()) {
return {.status = ParseStatus::kIncomplete, .next = index};
}
if (input_[index] == ']') {
++index;
return {.status = ParseStatus::kComplete, .next = index, .kind = JsonValueKind::kArray};
}
for (;;) {
ParseResult value = ParseValue(index, depth);
if (value.status != ParseStatus::kComplete) {
return value;
}
index = SkipWhitespace(input_, index);
if (index >= input_.size()) {
return {.status = ParseStatus::kIncomplete, .next = index};
}
const char ch = input_[index];
if (ch == ']') {
++index;
return {.status = ParseStatus::kComplete, .next = index, .kind = JsonValueKind::kArray};
}
if (ch != ',') {
return Error("expected ',' or ']' inside array", index);
}
++index;
index = SkipWhitespace(input_, index);
if (index >= input_.size()) {
return {.status = ParseStatus::kIncomplete, .next = index};
}
}
}
ParseResult ParseStringValue(std::size_t& index) {
ParseResult string_result = ParseString(index);
if (string_result.status != ParseStatus::kComplete) {
return string_result;
}
string_result.kind = JsonValueKind::kString;
return string_result;
}
ParseResult ParseString(std::size_t& index) {
if (index >= input_.size() || input_[index] != '"') {
return Error("expected string", index);
}
++index;
while (index < input_.size()) {
const unsigned char ch = static_cast<unsigned char>(input_[index]);
if (ch < 0x20u) {
return Error("control characters are not allowed in JSON strings", index);
}
if (ch == '"') {
++index;
return {.status = ParseStatus::kComplete, .next = index};
}
if (ch == '\\') {
++index;
if (index >= input_.size()) {
return {.status = ParseStatus::kIncomplete, .next = index};
}
const char escaped = input_[index];
switch (escaped) {
case '"':
case '\\':
case '/':
case 'b':
case 'f':
case 'n':
case 'r':
case 't':
++index;
break;
case 'u':
++index;
for (int count = 0; count < 4; ++count) {
if (index >= input_.size()) {
return {.status = ParseStatus::kIncomplete, .next = index};
}
if (!std::isxdigit(static_cast<unsigned char>(input_[index]))) {
return Error("invalid unicode escape in JSON string", index);
}
++index;
}
break;
default:
return Error("invalid escape sequence in JSON string", index);
}
continue;
}
++index;
}
return {.status = ParseStatus::kIncomplete, .next = index};
}
ParseResult ParseNumber(std::size_t& index) {
const std::size_t start = index;
if (input_[index] == '-') {
++index;
if (index >= input_.size()) {
return {.status = ParseStatus::kIncomplete, .next = index};
}
}
if (input_[index] == '0') {
++index;
} else if (input_[index] >= '1' && input_[index] <= '9') {
while (index < input_.size() && input_[index] >= '0' && input_[index] <= '9') {
++index;
}
} else {
return Error("invalid number", index);
}
if (index < input_.size() && input_[index] == '.') {
++index;
if (index >= input_.size()) {
return {.status = ParseStatus::kIncomplete, .next = index};
}
if (input_[index] < '0' || input_[index] > '9') {
return Error("invalid fraction in number", index);
}
while (index < input_.size() && input_[index] >= '0' && input_[index] <= '9') {
++index;
}
}
if (index < input_.size() && (input_[index] == 'e' || input_[index] == 'E')) {
++index;
if (index >= input_.size()) {
return {.status = ParseStatus::kIncomplete, .next = index};
}
if (input_[index] == '+' || input_[index] == '-') {
++index;
if (index >= input_.size()) {
return {.status = ParseStatus::kIncomplete, .next = index};
}
}
if (input_[index] < '0' || input_[index] > '9') {
return Error("invalid exponent in number", index);
}
while (index < input_.size() && input_[index] >= '0' && input_[index] <= '9') {
++index;
}
}
if (index == start) {
return Error("invalid number", index);
}
return {.status = ParseStatus::kComplete, .next = index, .kind = JsonValueKind::kNumber};
}
ParseResult ParseBoolean(std::size_t& index) {
if (input_.compare(index, 4, "true") == 0) {
index += 4;
return {.status = ParseStatus::kComplete, .next = index, .kind = JsonValueKind::kBoolean};
}
if (input_.compare(index, 5, "false") == 0) {
index += 5;
return {.status = ParseStatus::kComplete, .next = index, .kind = JsonValueKind::kBoolean};
}
if (input_.size() - index < 5 && std::string_view("false").substr(0, input_.size() - index) == input_.substr(index)) {
return {.status = ParseStatus::kIncomplete, .next = input_.size()};
}
if (input_.size() - index < 4 && std::string_view("true").substr(0, input_.size() - index) == input_.substr(index)) {
return {.status = ParseStatus::kIncomplete, .next = input_.size()};
}
return Error("invalid boolean literal", index);
}
ParseResult ParseNull(std::size_t& index) {
if (input_.compare(index, 4, "null") == 0) {
index += 4;
return {.status = ParseStatus::kComplete, .next = index, .kind = JsonValueKind::kNull};
}
if (input_.size() - index < 4 && std::string_view("null").substr(0, input_.size() - index) == input_.substr(index)) {
return {.status = ParseStatus::kIncomplete, .next = input_.size()};
}
return Error("invalid null literal", index);
}
ParseResult Error(std::string message, std::size_t index) const {
return {.status = ParseStatus::kError, .next = index, .error = std::move(message)};
}
std::string_view input_;
const AssemblerLimits& limits_;
std::size_t max_depth_observed_ = 0;
};
inline bool IsLikelyJsonStart(char ch, bool accept_top_level_primitives) {
if (ch == '{' || ch == '[') {
return true;
}
if (!accept_top_level_primitives) {
return false;
}
return ch == '"' || ch == '-' || ch == 't' || ch == 'f' || ch == 'n' || (ch >= '0' && ch <= '9');
}
inline std::size_t FindNextJsonStart(std::string_view input, std::size_t from, bool accept_top_level_primitives) {
for (std::size_t index = from; index < input.size(); ++index) {
if (IsLikelyJsonStart(input[index], accept_top_level_primitives)) {
return index;
}
}
return std::string_view::npos;
}
} // namespace detail
class JsonFragmentAssembler {
public:
explicit JsonFragmentAssembler(AssemblerLimits limits = {})
: limits_(limits) {}
std::vector<CompletedJsonValue> Feed(std::string_view chunk, bool from_sse) {
utf8_.Consume(chunk);
buffer_.append(chunk.data(), chunk.size());
EnforceBufferedJsonLimit();
std::vector<CompletedJsonValue> out;
for (;;) {
std::size_t start = detail::FindNextJsonStart(buffer_, 0, limits_.accept_top_level_primitives);
if (start == std::string_view::npos) {
stats_.discarded_noise_bytes += buffer_.size();
buffer_.clear();
break;
}
if (start > 0) {
stats_.discarded_noise_bytes += start;
buffer_.erase(0, start);
}
detail::JsonParser parser(buffer_, limits_);
detail::ParseResult result = parser.ParseSingleValue(0);
stats_.max_depth_observed = std::max(stats_.max_depth_observed, result.max_depth_observed);
if (result.status == detail::ParseStatus::kComplete) {
if (result.next > limits_.max_single_json_bytes) {
throw AssemblerError("single JSON payload exceeded configured limit");
}
out.push_back({
.json = buffer_.substr(0, result.next),
.kind = result.kind,
.from_sse = from_sse
});
buffer_.erase(0, result.next);
++stats_.completed_values;
if (out.size() > limits_.max_completed_queue) {
throw AssemblerError("completed JSON queue exceeded configured limit");
}
continue;
}
if (result.status == detail::ParseStatus::kIncomplete) {
if (buffer_.size() > limits_.max_single_json_bytes) {
throw AssemblerError("incomplete JSON payload exceeded configured limit");
}
break;
}
++stats_.discarded_invalid_candidates;
buffer_.erase(0, 1);
}
stats_.buffered_json_bytes = buffer_.size();
return out;
}
std::string_view PendingJson() const {
return buffer_;
}
const AssemblerStats& stats() const {
return stats_;
}
private:
void EnforceBufferedJsonLimit() {
if (buffer_.size() > limits_.max_buffered_json_bytes) {
throw AssemblerError("buffered JSON exceeded configured limit");
}
}
AssemblerLimits limits_;
detail::Utf8Validator utf8_;
std::string buffer_;
AssemblerStats stats_;
};
class AiSseJsonAssembler {
public:
explicit AiSseJsonAssembler(AssemblerLimits limits = {})
: limits_(limits),
json_assembler_(limits) {}
std::vector<CompletedJsonValue> FeedTransportBytes(std::string_view chunk) {
transport_utf8_.Consume(chunk);
transport_buffer_.append(chunk.data(), chunk.size());
EnforceTransportLimit();
std::vector<CompletedJsonValue> out;
std::size_t line_start = 0;
while (true) {
const std::size_t newline = transport_buffer_.find('\n', line_start);
if (newline == std::string::npos) {
transport_buffer_.erase(0, line_start);
stats_.buffered_transport_bytes = transport_buffer_.size();
return out;
}
std::string_view line(transport_buffer_.data() + line_start, newline - line_start);
if (!line.empty() && line.back() == '\r') {
line.remove_suffix(1);
}
line_start = newline + 1;
std::vector<CompletedJsonValue> line_out = ConsumeSseLine(line);
out.insert(out.end(),
std::make_move_iterator(line_out.begin()),
std::make_move_iterator(line_out.end()));
}
}
std::vector<CompletedJsonValue> FeedJsonText(std::string_view chunk) {
std::vector<CompletedJsonValue> out = json_assembler_.Feed(chunk, false);
SyncStats();
return out;
}
bool done() const {
return done_;
}
std::string_view PendingTransportBytes() const {
return transport_buffer_;
}
std::string_view PendingSseEventData() const {
return event_data_;
}
std::string_view PendingJson() const {
return json_assembler_.PendingJson();
}
AssemblerStats stats() const {
return stats_;
}
private:
std::vector<CompletedJsonValue> ConsumeSseLine(std::string_view line) {
std::vector<CompletedJsonValue> out;
if (line.empty()) {
DispatchEvent(out);
return out;
}
if (line[0] == ':') {
return out;
}
const std::size_t colon = line.find(':');
std::string_view field = colon == std::string_view::npos ? line : line.substr(0, colon);
std::string_view value = colon == std::string_view::npos ? std::string_view() : line.substr(colon + 1);
if (!value.empty() && value.front() == ' ') {
value.remove_prefix(1);
}
if (field == "data") {
if (!event_data_.empty()) {
event_data_.push_back('\n');
}
event_data_.append(value.data(), value.size());
return out;
}
if (field == "event") {
event_name_.assign(value.data(), value.size());
return out;
}
if (field == "id" || field == "retry") {
return out;
}
if (colon == std::string_view::npos && detail::Trim(line) == "[DONE]") {
done_ = true;
++stats_.sse_done_markers;
return out;
}
return out;
}
void DispatchEvent(std::vector<CompletedJsonValue>& out) {
if (event_data_.empty()) {
event_name_.clear();
return;
}
++stats_.sse_events_seen;
const std::string_view payload = detail::Trim(event_data_);
if (payload == "[DONE]") {
done_ = true;
++stats_.sse_done_markers;
event_data_.clear();
event_name_.clear();
return;
}
std::vector<CompletedJsonValue> event_out = json_assembler_.Feed(payload, true);
out.insert(out.end(),
std::make_move_iterator(event_out.begin()),
std::make_move_iterator(event_out.end()));
event_data_.clear();
event_name_.clear();
SyncStats();
}
void EnforceTransportLimit() {
if (transport_buffer_.size() > limits_.max_buffered_transport_bytes) {
throw AssemblerError("buffered transport bytes exceeded configured limit");
}
}
void SyncStats() {
const AssemblerStats& json_stats = json_assembler_.stats();
stats_.completed_values = json_stats.completed_values;
stats_.discarded_noise_bytes = json_stats.discarded_noise_bytes;
stats_.discarded_invalid_candidates = json_stats.discarded_invalid_candidates;
stats_.max_depth_observed = json_stats.max_depth_observed;
stats_.buffered_json_bytes = json_stats.buffered_json_bytes;
stats_.buffered_transport_bytes = transport_buffer_.size();
}
AssemblerLimits limits_;
detail::Utf8Validator transport_utf8_;
std::string transport_buffer_;
std::string event_name_;
std::string event_data_;
JsonFragmentAssembler json_assembler_;
bool done_ = false;
AssemblerStats stats_;
};
} // namespace vibe::streaming
/*
This solves the annoying April 2026 problem where AI gateways, edge workers, desktop copilots, and inference proxies receive tool-call arguments and structured outputs as broken-up Server-Sent Events instead of one clean JSON blob. Built because modern model APIs stream partial objects, arrays, and literals across many chunks, and production systems still need hard limits, UTF-8 safety, and deterministic completion before they can execute tools or persist results. Use it when you are wiring OpenAI-style, Anthropic-style, or homegrown SSE streams into a C++ service and you need a reusable parser that can survive noise before the JSON, incomplete frames, blank-line dispatch, and the common [DONE] sentinel. The trick: it separates transport assembly from JSON assembly, validates bytes as they arrive, trims only the noise you do not want, and keeps strict caps so one bad stream cannot quietly eat memory. Drop this into an inference gateway, agent runtime, streaming adapter, reverse proxy, embedded edge collector, or a desktop AI app that wants to turn fragmented event payloads into complete JSON values you can trust before acting on them.
*/