Implementation:AGenUI AGenUI IPlatformFunction
| Knowledge Sources | |
|---|---|
| Domains | Cross_Platform, UI_Framework |
| Last Updated | 2026-10-24 17:21 GMT |
Overview
Abstract interface for platform-side function implementations, bridging C++ core function call dispatch to native platform code (Android JNI, iOS ObjC, HarmonyOS ArkTS).
Description
The `IPlatformFunction` interface is the platform-side counterpart to `IFunctionCall`. While C++ builtins implement `IFunctionCall::execute()`, platform functions implement `IPlatformFunction::callSync()`. This interface is the bridge for host-registered custom functions that need platform-native capabilities (Android `toast`, iOS `UIAlertController`, network requests, etc.).
The interface has a single pure-virtual method:
- `callSync(context, params)`: receives a `FunctionCallContext` (instanceId + surfaceId) and a `params` JSON string, returns a `FunctionCallResult` (status + data string + error string).
The `FunctionCallManager` invokes `callSync` while holding its `recursive_mutex`, ensuring the raw `IPlatformFunction*` pointer remains valid — `unregisterFunctionCall` cannot erase the entry until `callSync` returns. The result is converted to `FunctionCallResolution` via `fromPlatformResult()`, which parses the `data` string as JSON.
`FunctionCallResult` has four status values: `Success`, `Error`, `Pending`, `Completed`. In the sync path, `Pending` is treated as an error (the sync dispatch cannot wait for async completion).
Usage
- Android: implement in Kotlin/Java, bridge via JNI (`jni_android_platform_function.cpp` provides the bridge pattern).
- iOS: implement in Swift/ObjC, bridge via ObjC++.
- HarmonyOS: implement in ArkTS, bridge via NAPI.
- Lifecycle: the caller owns the `IPlatformFunction*`; keep it alive until `unregisterFunction()` is called.
Code Reference
Source Location
- Repository: AGenUI
- Interface: `core/include/agenui_platform_function.h:L10-62`
- Android JNI bridge: `core/src/jni/jni_android_platform_function.cpp` (166 lines)
- Resolution conversion: `core/src/function_call/agenui_functioncall_resolution.h:L16-59`
Signature
// core/include/agenui_platform_function.h
namespace agenui {
enum class FunctionCallStatus {
Success, // Call succeeded
Error, // Call failed
Pending, // Async request submitted, awaiting callback
Completed // Async execution completed
};
struct FunctionCallResult {
FunctionCallStatus status = FunctionCallStatus::Error;
std::string data; // Return data on success (JSON string)
std::string error; // Error message on failure
};
struct FunctionCallContext {
int instanceId; // Surface manager instance ID
std::string surfaceId; // Surface unique identifier
};
class IPlatformFunction {
public:
virtual ~IPlatformFunction() = default;
// Invoke the function synchronously
// @param context Engine and surface context
// @param params Arguments as a JSON string
// @return Result with status, data (JSON string), and error message
virtual FunctionCallResult callSync(const FunctionCallContext& context,
const std::string& params) = 0;
};
} // namespace agenui
Import
#include "agenui_platform_function.h"
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| context | `FunctionCallContext` | Yes | {instanceId: int, surfaceId: string} |
| params | `const std::string&` | Yes | Function arguments as a JSON string |
Outputs
| Name | Type | Description |
|---|---|---|
| callSync returns | {status, data (JSON string), error} | |
| status | Success, Error, Pending, or Completed |
Usage Examples
Platform Function Implementation (C++ Test Mock)
#include "agenui_platform_function.h"
#include "nlohmann/json.hpp"
class MockToastFunction : public agenui::IPlatformFunction {
public:
agenui::FunctionCallResult callSync(
const agenui::FunctionCallContext& context,
const std::string& params) override {
auto args = nlohmann::json::parse(params, nullptr, false);
if (args.is_discarded() || !args.contains("message")) {
return {agenui::FunctionCallStatus::Error, "", "Missing 'message' arg"};
}
// In production, this would call native toast API
std::string message = args["message"];
printf("Toast: %s\n", message.c_str());
nlohmann::json result = {{"shown", true}};
return {agenui::FunctionCallStatus::Success, result.dump(), ""};
}
};
// Register with engine
MockToastFunction toastFunc;
std::string config = R"({"namespace":"agenui.platform","name":"toast",
"description":"Show toast","returnType":"object"})";
engine->registerFunction(config, &toastFunc);
Returning an Error
agenui::FunctionCallResult callSync(
const agenui::FunctionCallContext& context,
const std::string& params) override {
if (!hasPermission()) {
return {agenui::FunctionCallStatus::Error, "",
"Permission denied for toast"};
}
// ... success path
}