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 IAGenUIMessageListener

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

Overview

Abstract observer interface and event dispatcher for surface lifecycle, component diff, action, and error events, provided by the AGenUI C++ core.

Description

The `IAGenUIMessageListener` interface defines the callback contract for receiving surface events from the AGenUI engine. Platform renderers implement this interface to bridge C++ core events to native UI updates.

The interface has seven virtual callbacks (all default to no-op):

  • `onCreateSurface(msg)`: a new surface was created
  • `onDeleteSurface(msg)`: a surface was deleted
  • `onComponentsUpdate(surfaceId, messages)`: component attributes changed (diff)
  • `onComponentsAdd(surfaceId, messages)`: new components were added
  • `onComponentsRemove(surfaceId, messages)`: components were removed
  • `onActionEventRouted(content)`: a user action was triggered (for inter-listener forwarding)
  • `onError(msg)`: an SDK error occurred (code, surfaceId, message)

The `EventDispatcher` manages the listener list under a `std::mutex` and fans out events. Listeners are registered via `ISurfaceManager::addSurfaceEventListener()`. A cached-listener mechanism handles the race where a listener is added before the dispatcher is initialized: the listener is cached and flushed to the dispatcher when `SurfaceManager::init()` completes, all under the same `_cachedListenersMutex`.

All callbacks are invoked on the shared worker thread, not the main thread. Platform implementations must marshal to the main/UI thread for native UI updates.

Usage

  • Platform renderer: implement all callbacks to drive native UI (View creation, RecyclerView diffing, etc.)
  • Analytics: implement a second listener to observe user actions and errors for telemetry
  • Testing: use a mock listener (e.g., `mock_message_listener.h` in tests) to capture events

Code Reference

Source Location

  • Repository: AGenUI
  • Listener interface: `core/include/agenui_message_listener.h:L11-61`
  • EventDispatcher: `core/src/module/agenui_event_dispatcher.cpp:L6-107`
  • Dispatcher header: `core/src/module/agenui_event_dispatcher.h`
  • Registration: `core/src/module/agenui_surface_manager.cpp:L77-107`

Signature

// core/include/agenui_message_listener.h
namespace agenui {

class IAGenUIMessageListener {
public:
    virtual ~IAGenUIMessageListener() {}

    virtual void onCreateSurface(const CreateSurfaceMessage& msg) {}
    virtual void onDeleteSurface(const DeleteSurfaceMessage& msg) {}
    virtual void onComponentsUpdate(const std::string& surfaceId,
                                    const std::vector<ComponentsUpdateMessage>& msg) {}
    virtual void onComponentsAdd(const std::string& surfaceId,
                                 const std::vector<ComponentsAddMessage>& msg) {}
    virtual void onComponentsRemove(const std::string& surfaceId,
                                    const std::vector<ComponentsRemoveMessage>& msg) {}
    virtual void onActionEventRouted(const std::string& content) {}
    virtual void onError(const ErrorMessage& msg) {}
};

// EventDispatcher fan-out methods
class EventDispatcher {
    void addEventListener(IAGenUIMessageListener* listener);
    void removeEventListener(IAGenUIMessageListener* listener);
    void removeAllEventListeners();
    void dispatchCreateSurface(const CreateSurfaceMessage& msg);
    void dispatchDeleteSurface(const DeleteSurfaceMessage& msg);
    void dispatchComponentsUpdate(const std::string& surfaceId,
                                  const std::vector<ComponentsUpdateMessage>& messages);
    void dispatchComponentsAdd(const std::string& surfaceId,
                               const std::vector<ComponentsAddMessage>& messages);
    void dispatchComponentsRemove(const std::string& surfaceId,
                                  const std::vector<ComponentsRemoveMessage>& messages);
    void dispatchActionEventRouted(const std::string& content);
    void dispatchError(const ErrorMessage& msg);
};

} // namespace agenui

Import

#include "agenui_message_listener.h"
// For dispatcher: #include "module/agenui_event_dispatcher.h"

I/O Contract

Inputs

Name Type Required Description
listener `IAGenUIMessageListener*` Yes Observer pointer; lifecycle managed by caller

Outputs

Name Type Description
onCreateSurface `CreateSurfaceMessage` Surface ID, raw protocol content
onComponentsUpdate `string + vector<ComponentsUpdateMessage>` Surface ID + component diffs
onActionEventRouted `string` Action event content (data binding already resolved)
onError `ErrorMessage` Error code, surfaceId, message string

Usage Examples

Basic Platform Listener

#include "agenui_message_listener.h"

class MyRenderer : public agenui::IAGenUIMessageListener {
public:
    void onCreateSurface(const agenui::CreateSurfaceMessage& msg) override {
        // Create native container for this surface
        printf("Create surface: %s\n", msg.surfaceId.c_str());
    }

    void onComponentsUpdate(const std::string& surfaceId,
                            const std::vector<agenui::ComponentsUpdateMessage>& msg) override {
        // Update native UI with component diffs
        for (const auto& comp : msg) {
            updateNativeComponent(comp);
        }
    }

    void onComponentsAdd(const std::string& surfaceId,
                         const std::vector<agenui::ComponentsAddMessage>& msg) override {
        // Add new native components
    }

    void onError(const agenui::ErrorMessage& msg) override {
        // Report error to crash reporter
        printf("Error %d on %s: %s\n", msg.code, msg.surfaceId.c_str(), msg.message.c_str());
    }
};

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

Related Pages

Implements Principle

Requires Environment

Uses Heuristic

Page Connections

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