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 createSurfaceManager

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

Overview

Concrete API for creating, destroying, and managing multi-instance surface managers on the AGenUI engine, provided by the AGenUI C++ core.

Description

The `createSurfaceManager()` method on `IAGenUIEngine` creates a new independent surface rendering context. Each `SurfaceManager` instance owns its own virtual DOM, data model, event callbacks, streaming parser, and surface coordinator — all fully isolated from other instances.

Creation is thread-safe: the engine assigns a sequential `instanceId` via `std::atomic<int>::fetch_add`, creates a `shared_ptr<SurfaceManager>`, stores it in a mutex-protected map, then posts `sm->init()` to the shared worker thread. The `init()` method creates the `EventDispatcher`, `SurfaceCoordinator`, and `StreamingContentParser`.

The engine also provides `destroySurfaceManager()` (erases from map, posts `uninit()` to worker thread) and `findSurfaceManagerShared(instanceId)` (returns a `shared_ptr` keeping the surface alive until the caller releases it, serialized with destroy under the same lock).

Usage

  • Multiple chat windows: create one surface manager per independent chat session.
  • Side panel + main view: create separate surface managers for each rendering area.
  • Dynamic surfaces: create and destroy at runtime as the user navigates.
  • Cross-surface isolation: each surface can receive a different LLM stream independently.

Code Reference

Source Location

  • Repository: AGenUI
  • Interface: `core/include/agenui_engine.h:L29-48`
  • Engine impl: `core/src/module/agenui_engine_impl.cpp:L89-148`
  • SurfaceManager: `core/src/module/agenui_surface_manager.cpp:L13-57`
  • ISurfaceManager: `core/include/agenui_surface_manager_interface.h:L13-145`

Signature

// core/include/agenui_engine.h
class IAGenUIEngine {
    // Create a new surface manager instance
    virtual ISurfaceManager* createSurfaceManager() = 0;

    // Destroy a surface manager instance
    virtual void destroySurfaceManager(ISurfaceManager* surfaceManager) = 0;

    // Find by instanceId with shared ownership (thread-safe)
    virtual std::shared_ptr<ISurfaceManager> findSurfaceManagerShared(int instanceId) = 0;
};

// core/include/agenui_surface_manager_interface.h
class ISurfaceManager {
    virtual int getInstanceId() const = 0;
    virtual void addSurfaceEventListener(IAGenUIMessageListener* listener) = 0;
    virtual void removeSurfaceEventListener(IAGenUIMessageListener* listener) = 0;
    virtual void beginTextStream() = 0;
    virtual void receiveTextChunk(const std::string& data) = 0;
    virtual void endTextStream() = 0;
    virtual void submitUIAction(const ActionMessage& msg) = 0;
    virtual void submitUIDataModel(const SyncUIToDataMessage& msg) = 0;
    virtual void invalidateFunctionCallValues() = 0;
    // ... (full interface in source)
};

Import

#include "agenui_engine.h"
#include "agenui_surface_manager_interface.h"

I/O Contract

Inputs

Name Type Required Description
(createSurfaceManager) — — No parameters; auto-assigns sequential instanceId
surfaceManager `ISurfaceManager*` Yes (destroy) Pointer returned by createSurfaceManager
instanceId `int` Yes (find) Unique ID assigned at creation

Outputs

Name Type Description
createSurfaceManager() returns `ISurfaceManager*` New surface manager, or nullptr if engine not running
findSurfaceManagerShared() returns `shared_ptr<ISurfaceManager>` Shared ownership or nullptr if not found
SurfaceManager internals Various EventDispatcher, SurfaceCoordinator, StreamingContentParser (created on worker thread)

Usage Examples

Creating Multiple Surfaces

#include "agenui_engine_entry.h"
#include "agenui_engine.h"
#include "agenui_surface_manager_interface.h"

auto* engine = agenui::initAGenUIEngine();

// Create independent surface managers
auto* chatSurface = engine->createSurfaceManager();    // instanceId=0
auto* panelSurface = engine->createSurfaceManager();   // instanceId=1

// Each surface receives its own LLM stream
chatSurface->beginTextStream();
chatSurface->receiveTextChunk(chatStreamData);

panelSurface->beginTextStream();
panelSurface->receiveTextChunk(panelStreamData);

// Clean up
chatSurface->endTextStream();
panelSurface->endTextStream();
engine->destroySurfaceManager(chatSurface);
engine->destroySurfaceManager(panelSurface);

Thread-Safe Surface Lookup

// Find a surface by ID with shared ownership (keeps it alive)
auto shared = engine->findSurfaceManagerShared(0);
if (shared) {
    shared->invalidateFunctionCallValues();
}
// shared_ptr releases; surface stays alive in engine map

Related Pages

Implements Principle

Requires Environment

Uses Heuristic

Page Connections

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