Principle:AGenUI AGenUI Function Call Dispatch System
| 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:
- 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.
- 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
- Implementation:AGenUI_AGenUI_FunctionCallManager
- Implementation:AGenUI_AGenUI_IFunctionCall
- Implementation:AGenUI_AGenUI_IPlatformFunction
- Implementation:AGenUI_AGenUI_registerFunction
- Implementation:AGenUI_AGenUI_FunctionCallConfig
- Implementation:AGenUI_AGenUI_FunctionCallResolution
- Implementation:AGenUI_AGenUI_FormatDateFunctionCall