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

Principle:AGenUI AGenUI Function Call Dispatch System

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

Overview

A dual-layer function dispatch system that resolves named function calls by first checking C++ built-in implementations and then falling back to platform-registered functions, enabling both pre-built utility functions and host-defined custom operations.

Description

The Command pattern encapsulates an operation as an object with a unified `execute` interface, decoupling the requester from the receiver. AGenUI uses this to evaluate functionCall expressions embedded in A2UI protocol JSON — expressions like `{"call": "formatCurrency", "args": {"value": 99.5}}` are resolved at render time to produce dynamic values.

The system has two dispatch layers:

  1. C++ built-in functions (`IFunctionCall` subclasses): registered as `shared_ptr` in `_cppFunctionCalls`. These include formatters (`formatString`, `formatNumber`, `formatCurrency`, `formatDate`), validators (`required`, `regex`, `length`, `numeric`, `email`), logic operators (`and`, `or`, `not`), and `resolveCalls` for recursive resolution. They execute directly in C++ with no platform round-trip.
  2. Platform functions (`IPlatformFunction` implementations): registered by the host app via `registerFunction()`, stored as raw `IPlatformFunction*` in `_functionCalls`. These bridge to platform-native code (e.g., Android `toast()`) via JNI/ObjC/ArkTS.

The `FunctionCallManager::executeFunctionCallSync()` method performs ordered lookup: C++ first, then platform. This lets built-in functions override platform functions of the same name if needed. Execution is protected by a `std::recursive_mutex` to allow `resolveCalls` to recursively invoke the manager.

Usage

Use this principle when:

  • A UI protocol embeds function-call expressions that must be evaluated at render time
  • The framework needs built-in utility functions (format, validate) plus host-extensible custom functions
  • Function calls must be dispatched synchronously during component attribute resolution
  • Some functions need to recursively resolve nested function calls

Theoretical Basis

The dual-layer dispatch algorithm:

executeFunctionCallSync(name, context, args):
    1. Look up name in _cppFunctionCalls (C++ builtins, shared_ptr)
       → if found: return cppFunctionCall->execute(args, context)
    2. Look up name in _functionCalls (platform functions, raw ptr)
       → if found: return fromPlatformResult(function->callSync(context, args.dump()))
    3. Return Error("FunctionCall not found: name")

The registration model:

# Abstract registration (NOT real implementation)
# C++ builtin: shared ownership, safe lifetime
manager.registerFunctionCall(shared_ptr<IFunctionCall>)

# Platform function: raw pointer, caller owns lifecycle
manager.registerFunctionCall(config_json, IPlatformFunction*)

# Lookup by short name ("toast" not "agenui.platform::toast")
_functionCalls["toast"] = {config, function_ptr}

Practical Guide

  • Built-in functions (15) are registered automatically during `SurfaceCoordinator` construction.
  • Register custom platform functions via `engine->registerFunction(configJson, &myFunction)`.
  • The config JSON must include `name`, `namespace`, `description`, and `returnType`.
  • Unregister with `engine->unregisterFunction(name)` when the function is no longer needed.
  • Keep the `IPlatformFunction*` alive until unregistered; the manager holds a raw pointer.

Related Pages

Implemented By

Uses Heuristic

Page Connections

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