English
callAgent
About 915 wordsAbout 3 min
2026-10-16
This method is used to invoke an Agent and poll for the execution result at a fixed interval after the task is submitted. Pass business parameters through data and optional settings through options. The method returns a Promise.
Syntax: FxUI.biz.callAgent(data, options)
Parameters
| Parameter | Description | Type | Allowed Values | Default |
|---|---|---|---|---|
| data | Business parameters (required) | Object | — | — |
| options | Additional options | Object | — | {} |
data
| Parameter | Description | Type | Allowed Values | Default |
|---|---|---|---|---|
| apiName | API name of the Agent | string | — | ShareAgent |
| content | Message content, see the example for the item structure | Array | — | [] |
| sessionId | Session ID | string | — | {apiName}__{random UUID} |
| instanceId | Instance ID | string | — | Same as sessionId |
options
| Parameter | Description | Type | Allowed Values | Default |
|---|---|---|---|---|
| signal | Abort signal object used to abort the polling call. It must contain an aborted boolean field (an abort() method is optional). The miniprogram runtime has no AbortController; do not pass the web AbortSignal | Object | — | — |
| method | Execution mode. Pass EventSource to use the SSE streaming mode (see "EventSource Streaming Mode" below); otherwise the polling mode is used | string | EventSource | Polling mode |
When signal is passed, the method checks whether the call has been aborted before the request is sent and after the execution result is submitted.
Example
Get the execution result with the polling mode:
import FxUI from 'fxui-mobile';
FxUI.biz.callAgent({
apiName: 'ShareAgent',
content: [
{
type: 'text',
text: 'Query the weather in Beijing today'
},
{
type: 'file',
source: {
path: 'TN_de531073e66c4217b4f17146f0b34f60',
filename: '(This is the class)MRCRMSyncUtil.groovy',
size: 14476,
ext: 'groovy'
}
},
{
type: 'image',
source: {
path: 'TN_7d6da51cb3c8485887631ca618e1e2a9',
filename: '2.Shortcut recommendation configuration.png',
size: 120619,
ext: 'png'
}
}
]
}).then((res) => {
console.log(res);
}).catch((err) => {
console.log(err);
});Abort the call:
import FxUI from 'fxui-mobile';
// The miniprogram runtime has no AbortController; maintain a signal object with an aborted field yourself
const signal = {
aborted: false,
abort() {
this.aborted = true;
}
};
FxUI.biz.callAgent({
apiName: 'ShareAgent',
content: []
}, {
signal
}).catch((err) => {
console.log(err);
});
// Call this when you need to abort
signal.abort();Return Value (Polling Mode)
The method submits the execution first and then polls the execution result every 2 seconds until the task is no longer in the running state. The resolved result structure is as follows:
| Field | Description | Type |
|---|---|---|
| code | Execution status. When it is POLLING, polling continues; any other status value means the task has finished | string |
| data | Execution result data | — |
{
code: 'POLLING',
data: {}
}On failure, the Promise rejects with the failed call result:
{
success: false,
error: {}
}If the execution result is submitted successfully but the returned success is not truthy, the Promise rejects with the raw result of the execution submission.
When the call is aborted through signal, the Promise rejects with the error aborted.
EventSource Streaming Mode
When options.method is EventSource, the streaming mode is used: the method first requests the completion API and then continuously receives event increments over SSE (chunked transfer) instead of polling. If the connection drops unexpectedly, it automatically reconnects with the event sequence number (up to 3 times, 1 second apart).
This mode also returns a Promise and exposes process events through callbacks on options:
| Callback | Description |
|---|---|
| onOpen | Triggered once when the SSE connection is established. The argument is { messageId, stop(), abort() }. stop() reports the stop to the backend (user_stop) before closing the connection; abort() closes the connection directly |
| onMessage | Triggered for each server event. The argument is { event, id, data }; data is automatically JSON-parsed (the backend may serialize twice, which is handled as well) |
| onQuestion | Triggered when the Agent needs the user to supply more information (question / userInteractionForm events). The argument is { question, resolveQuestion(answer), rejectQuestion }. Calling resolveQuestion(answer) continues this round with the execution context; calling rejectQuestion ends the call with an error. If this callback is not provided, receiving a question event ends the call directly |
| onClose | Triggered when the connection ends non-abruptly |
| onFinish | Triggered when the finish event is received and the call ends normally |
| onError | Triggered when the connection errors and retries are exhausted; the Promise rejects at the same time |
| onTraceId | Triggered after each SSE connection is established. The argument is the accumulated list of traceIds, for tracking purposes |
Return value: on normal completion the Promise resolves (with no business value); when interrupted through the stop() / abort() exposed by onOpen, it resolves with 'aborted'.
import FxUI from 'fxui-mobile';
FxUI.biz.callAgent({
apiName: 'ShareAgent',
content: [{ type: 'text', text: 'Query the weather in Beijing today' }]
}, {
method: 'EventSource',
onOpen(info) {
console.log('connected', info.messageId);
// To stop: info.stop() or info.abort()
},
onMessage(msg) {
console.log('event', msg.event, msg.data);
},
onFinish() {
console.log('finished');
}
});Notes
- The polling interval of this method is fixed at 2 seconds, and polling stops when the execution status is no longer
POLLING. - Fields already present in
dataoverride the defaults in the table above. IfsessionIdandinstanceIdare not passed, a new session ID and instance ID are generated asapiName__random UUIDon every call. sessionIdis generated from theapiNameyou pass. IfapiNameis not passed, the prefix isShareAgent.- The abort check of the polling mode happens before and after the execution is submitted; during polling,
options.signalis passed along with the internal calls.signaldoes not take effect in EventSource mode; use thestop()/abort()exposed by theonOpencallback to interrupt the streaming mode. - This method requires
fxui-mobile@20261010.0.2or later. - The polling mode is implemented with the
ai-asset agent executeandai-asset agent find-execute-resultoperations in the Operation Center. You can look up the related operations in the Operation Library.
