Implementation:AGenUI AGenUI FunctionCallManager
| Knowledge Sources | |
|---|---|
| Domains | Software_Architecture, UI_Framework |
| Last Updated | 2026-10-24 17:21 GMT |
Overview
Concrete registry and dispatch manager for function calls, providing ordered C++ builtin and platform function lookup, synchronous execution, and thread-safe registration/unregistration.
Description
The `FunctionCallManager` is the central registry for all function calls in the AGenUI SDK. It maintains two maps:
- `_cppFunctionCalls`: maps short name → `shared_ptr<IFunctionCall>` (C++ built-in functions, shared ownership).
- `_functionCalls`: maps short name → `FunctionCallEntry{config, IPlatformFunction*}` (platform functions, raw pointer owned by caller).
Both maps are protected by a `std::recursive_mutex`, allowing `resolveCalls` to recursively invoke `executeFunctionCallSync` during execution.
Registration:
- `registerFunctionCall(FunctionCallPtr)`: registers a C++ builtin via `shared_ptr`. The 15 builtins are registered automatically by `SurfaceCoordinator::initFunctionCalls()`.
- `registerFunctionCall(FunctionCallConfig, IPlatformFunction*)`: registers a platform function. Config is parsed from JSON via `FunctionCallConfig::fromJson()`.
Execution:
- `executeFunctionCallSync(name, context, args)`: ordered lookup — C++ first, then platform. C++ calls invoke `IFunctionCall::execute(args, context)`; platform calls invoke `IPlatformFunction::callSync(context, args.dump())` then convert via `FunctionCallResolution::fromPlatformResult()`. `Pending` status is treated as error in the sync path.
Usage
- Host registration: call `engine->registerFunction(configJson, &myFunction)` for custom platform functions.
- Built-in functions: 15 functions are auto-registered; no host action needed.
- Expression evaluation: called internally during component attribute resolution when `{"call": "name", ...}` is encountered.
Code Reference
Source Location
- Repository: AGenUI
- Header: `core/src/function_call/agenui_functioncall_manager.h:L31-99`
- Implementation: `core/src/function_call/agenui_functioncall_manager.cpp:L1-158`
Signature
// core/src/function_call/agenui_functioncall_manager.h
namespace agenui {
class FunctionCallManager {
public:
// Register a platform function (raw pointer, caller owns lifecycle)
bool registerFunctionCall(const FunctionCallConfig& config,
IPlatformFunction* function);
// Register a C++ builtin function (shared_ptr, shared ownership)
bool registerFunctionCall(FunctionCallPtr functionCall);
// Unregister a platform function by name
bool unregisterFunctionCall(const std::string& name);
// Execute a function call synchronously (ordered: C++ first, then platform)
FunctionCallResolution executeFunctionCallSync(
const std::string& name,
const FunctionCallContext& context,
const nlohmann::json& args);
// Get all registered function calls (both C++ and platform)
std::vector<FunctionCallConfig> getAllFunctionCalls() const;
// Export function call catalog as JSON
nlohmann::json exportCatalog() const;
};
} // namespace agenui
Import
#include "function_call/agenui_functioncall_manager.h"
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| config | `FunctionCallConfig` | Yes | Function metadata (namespace, name, description, returnType) |
| function | `IPlatformFunction*` | Yes | Platform function impl (caller owns lifecycle) |
| functionCall | `FunctionCallPtr` | Yes | C++ builtin as shared_ptr<IFunctionCall> |
| name | `const std::string&` | Yes | Short function name for lookup/execution |
| context | `FunctionCallContext` | Yes | {instanceId, surfaceId} |
| args | `const nlohmann::json&` | Yes | Function arguments as JSON object |
Outputs
| Name | Type | Description |
|---|---|---|
| executeFunctionCallSync returns | Status (Success/Error) + JSON value + error message | |
| getAllFunctionCalls returns | All registered function configs | |
| exportCatalog returns | Full catalog as JSON object |
Usage Examples
Registering a Platform Function
#include "agenui_engine.h"
#include "agenui_platform_function.h"
class ToastFunction : public agenui::IPlatformFunction {
public:
agenui::FunctionCallResult callSync(
const agenui::FunctionCallContext& context,
const std::string& params) override {
// params is a JSON string: {"message": "...", "duration": 3000}
auto json = nlohmann::json::parse(params);
showNativeToast(json["message"], json.value("duration", 3000));
return {agenui::FunctionCallStatus::Success, "{}", ""};
}
};
// Register with the engine
auto* engine = agenui::getAGenUIEngine();
ToastFunction toastFunc;
std::string config = R"({"namespace":"agenui.platform","name":"toast",
"description":"Show a toast","returnType":"object"})");
engine->registerFunction(config, &toastFunc);
// Now A2UI protocol can use {"call":"toast","args":{"message":"Hi!"}}
Querying Registered Functions
auto* engine = agenui::getAGenUIEngine();
auto* fcManager = engine->getFunctionCallManager(); // (via engine context)
auto catalog = fcManager->exportCatalog();
printf("Registered functions: %s\n", catalog.dump(2).c_str());