Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Implementation:AGenUI AGenUI ISurfaceManager Streaming

From Leeroopedia
Knowledge Sources
Domains Streaming, UI_Framework
Last Updated 2026-10-24 17:21 GMT

Overview

Concrete streaming API on `ISurfaceManager` for feeding LLM-generated A2UI protocol data in chunks through the streaming pipeline, provided by the AGenUI C++ core.

Description

The streaming API on `ISurfaceManager` consists of three methods that form a session lifecycle: `beginTextStream()`, `receiveTextChunk(data)`, and `endTextStream()`. Together they implement a begin → receive × N → end contract for incremental protocol data processing.

Each method checks `_isRunning`, captures a `shared_ptr` to `self` (ensuring the surface stays alive during async processing), and posts a lambda to the shared worker thread:

  • `beginTextStream()` → `StreamingContentParser::processDataBeginning()` — resets the `ProtocolStreamExtractor` buffer and state machine to `Idle`.
  • `receiveTextChunk(data)` → `StreamingContentParser::processDataAssembling(data)` — appends data to the buffer, drives the parser, and dispatches parse results (batched).
  • `endTextStream()` → `StreamingContentParser::processDataEnding()` — checks for residual data, resets state to `Idle`.

The recommended call sequence is `beginTextStream → receiveTextChunk × N → endTextStream`. If `receiveTextChunk` is called without `beginTextStream`, the SDK still works (compatibility mode), but residual state from the previous round may not be cleaned up.

Usage

  • SSE streaming: call `beginTextStream()` on connection open, `receiveTextChunk()` for each SSE event, `endTextStream()` on stream close.
  • Chunked HTTP: call `beginTextStream()` before the first chunk, `receiveTextChunk()` per chunk, `endTextStream()` on response end.
  • User abort: call `endTextStream()` to clean up state when the user cancels mid-stream.
  • Network disconnect: call `endTextStream()` to flush and reset after abnormal disconnection.

Code Reference

Source Location

  • Repository: AGenUI
  • Interface: `core/include/agenui_surface_manager_interface.h:L64-103`
  • Implementation: `core/src/module/agenui_surface_manager.cpp:L143-197`
  • ContentParser: `core/src/stream/agenui_streaming_content_parser.cpp:L35-57`

Signature

// core/include/agenui_surface_manager_interface.h
class ISurfaceManager {
    // Begin a streaming session (reset buffer and parser state)
    virtual void beginTextStream() = 0;

    // Feed a text chunk of A2UI protocol data
    virtual void receiveTextChunk(const std::string& data) = 0;

    // End the streaming session (flush residual, reset state)
    virtual void endTextStream() = 0;
};

Import

#include "agenui_surface_manager_interface.h"

I/O Contract

Inputs

Name Type Required Description
data `const std::string&` Yes (receiveTextChunk) A2UI protocol data chunk (text string)

Outputs

Name Type Description
(void) — All three methods return void; results surface via IAGenUIMessageListener callbacks
Parse results Internal ParseResults dispatched to SurfaceCoordinator (createSurface, updateComponents, updateDataModel, deleteSurface)
Render callbacks Via listener onComponentsUpdate, onComponentsAdd, onCreateSurface triggered on worker thread

Usage Examples

Full Streaming Session

auto* sm = engine->createSurfaceManager();
sm->addSurfaceEventListener(&renderer);

// 1. Begin streaming session
sm->beginTextStream();

// 2. Feed chunks as they arrive from the LLM
for (const auto& chunk : llmStreamChunks) {
    sm->receiveTextChunk(chunk);
}

// 3. End streaming session
sm->endTextStream();

// Renderer received incremental component updates via callbacks

Error Recovery

sm->beginTextStream();
try {
    // Feed chunks...
    sm->receiveTextChunk(data);
} catch (...) {
    // Abnormal condition: clean up state
    sm->endTextStream();  // resets to Idle, ready for next session
    throw;
}

Related Pages

Implements Principle

Requires Environment

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment