Implementation:AGenUI AGenUI ProtocolStreamExtractor
| Knowledge Sources | |
|---|---|
| Domains | Parsing, Streaming |
| Last Updated | 2026-10-24 17:21 GMT |
Overview
Streaming JSON protocol state machine that accumulates text chunks, extracts complete A2UI protocol events and individual components incrementally, and emits parse results for downstream processing.
Description
The `ProtocolStreamExtractor` is a 1170-line state machine that implements incremental JSON parsing for the A2UI v0.9 protocol. It maintains an internal `_dataBuffer` (max 10 MB) and a `StreamState` (`Idle`, `StreamingComponents`, `StreamingDataModel`).
The two primary entry points are:
- `appendData(data)`: appends a new text chunk to the buffer (with overflow protection).
- `driveParser()`: processes the buffer and returns a `vector<ParseResult>`.
The parser supports four A2UI event types: `CreateSurface`, `UpdateComponents`, `UpdateDataModel`, `AppendDataModel`, and `DeleteSurface`. For `UpdateComponents`, it doesn't wait for the full components array — it extracts individual component JSON objects as their braces close, enabling real-time per-component rendering. For `UpdateDataModel`, it emits field-level incremental updates.
An optional stream plugin (`IStreamPlugin`) allows content-specific streaming strategies. The `CompositeStreamPlugin` chains `MarkdownStreamPlugin` and `TextStreamPlugin` for markdown field-level incremental delivery.
`ParseResult` has two types:
- `NormalEvent`: a complete non-streaming event (createSurface, updateDataModel, deleteSurface) with `eventJson`.
- `ComponentUpdate`: a single component object from updateComponents with `componentJson`, `surfaceId`, `version`.
Usage
- Internal to SDK: called by `StreamingContentParser`; not directly called by host code.
- Testing: unit-tested extensively in `tests/cpp/stream/` with chunking, datamodel, and markdown integration tests.
Code Reference
Source Location
- Repository: AGenUI
- Header: `core/src/stream/agenui_protocol_stream_extractor.h:L20-173`
- Implementation: `core/src/stream/agenui_protocol_stream_extractor.cpp` (1170 lines)
Signature
// core/src/stream/agenui_protocol_stream_extractor.h
namespace agenui {
class ProtocolStreamExtractor {
public:
enum class EventType {
Unknown, CreateSurface, UpdateComponents,
UpdateDataModel, AppendDataModel, DeleteSurface
};
struct ParseResult {
enum class Type { NormalEvent, ComponentUpdate };
Type type = Type::NormalEvent;
EventType eventType = EventType::Unknown;
std::string eventJson; // NormalEvent: complete event JSON
std::string componentJson; // ComponentUpdate: single component JSON
std::string surfaceId; // ComponentUpdate
std::string version; // ComponentUpdate
};
void setPlugin(IStreamPlugin* plugin);
void appendData(const std::string& data);
std::vector<ParseResult> driveParser();
bool hasUnprocessedData() const;
void reset();
static bool extractFirstCompleteJson(const std::string& buffer,
std::string& outJson, size_t& outEndPos);
static bool isJsonObjectComplete(const std::string& json,
size_t startPos, size_t& endPos);
EventType detectEventType(const std::string& data) const;
};
} // namespace agenui
Import
#include "stream/agenui_protocol_stream_extractor.h"
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| data | `const std::string&` | Yes | Text chunk to append to internal buffer |
| plugin | `IStreamPlugin*` | No | Optional streaming plugin (text/markdown/composite) |
Outputs
| Name | Type | Description |
|---|---|---|
| driveParser() returns | Parsed events and component updates | |
| ParseResult.type | `Type` enum | NormalEvent (complete event) or ComponentUpdate (individual component) |
| ParseResult.eventType | `EventType` | createSurface, updateComponents, updateDataModel, appendDataModel, deleteSurface |
Usage Examples
Manual Protocol Extraction
#include "stream/agenui_protocol_stream_extractor.h"
agenui::ProtocolStreamExtractor extractor;
// Feed chunks
extractor.appendData(R"({"type":"createSurface","surfaceId":"s1"})");
extractor.appendData(R"({"type":"updateComponents","surfaceId":"s1","components":[{"component":"Text","id":"t1","text":"Hello"}]})");
// Drive parser
auto results = extractor.driveParser();
for (const auto& r : results) {
if (r.type == agenui::ProtocolStreamExtractor::ParseResult::Type::NormalEvent) {
printf("Event: %s\n", r.eventJson.c_str());
} else {
printf("Component: %s\n", r.componentJson.c_str());
}
}
// Reset for next session
extractor.reset();