Function components include the icon component and button component. When no title is set, the icon component is displayed by default. When a title is set, the button component is displayed by default.
Icon component: functions as a comprehensive entry. It contains no user intent and can serve as the main entry point for the in-app agent.
Button component: allows the app to define a custom function description.
Develop an agent.
Associate the agent with an app.
Ensure that a HUAWEI ID is signed in on the device and that the device is connected to the Internet.
In the /src/main/ets/pages/Index.ets file of the project root directory, import FunctionComponent and related classes.
import { FunctionComponent, FunctionController } from '@kit.AgentFrameworkKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { common } from '@kit.AbilityKit'; (Optional) Call isAgentSupport to check whether the current agent ID is available before component loading. Load the component only when the agent ID is available and the agent function is supported.
@State isAgentSupport: boolean = false;
aboutToAppear() {
this.checkAgentSupport()
}
async checkAgentSupport() {
try {
let context = this.getUIContext()?.getHostContext() as common.UIAbilityContext;
this.isAgentSupport = await this.controller.isAgentSupport(context, this.agentId)
} catch (err) {
hilog.error(0x0001, 'AgentExample', `err code: ${err.code}, message: ${err.message}`)
}
}
build() {
Column() {
if (this.isAgentSupport) {
FunctionComponent({
agentId: this.agentId,
onError: (err: BusinessError) => {
hilog.error(0x0001, 'AgentExample', `err: ${JSON.stringify(err)}, message: ${err.message}`);
},
options: {
title: 'Intelligent creation',
queryText: 'Create a pattern'
}
})
}
}
} Build a simple configuration page, import FunctionComponent to the page, and pass the required parameters. The agentId and onError parameters are mandatory. For details about other optional parameters, please refer to FunctionComponent. For details about the Function component layout, please refer to Component Layouts.
@Entry
@Component
export struct AgentExample {
private controller: FunctionController = new FunctionController();
private agentId: string = 'agentproxy65481da1fa2293a8482d45'; // Agent ID, which is specified by the Celia agent platform when the agent is created.
build() {
Column() {
FunctionComponent({
agentId: this.agentId,
onError: (err: BusinessError) => {
hilog.error(0x0001, 'AgentExample', `err: ${JSON.stringify(err)}, message: ${err.message}`);
},
options: {
title: '',
queryText: ''
},
controller: this.controller
})
}
}
} Add a subscription event.
aboutToAppear() {
this.initListeners();
}
initListeners() {
this.controller?.on('agentDialogOpened', this.onAgentOpenedCallback);
this.controller?.on('agentDialogClosed', this.onAgentClosedCallback);
}
onAgentOpenedCallback = () => {
hilog.info(0x0001, 'AgentExample', 'agent dialog opened callback');
};
onAgentClosedCallback = () => {
hilog.info(0x0001, 'AgentExample', 'agent dialog closed callback');
};
aboutToDisappear() {
this.controller?.off('agentDialogOpened');
this.controller?.off('agentDialogClosed');
}
build() {
Column() {
FunctionComponent({
agentId: this.agentId,
onError: (err: BusinessError) => {
hilog.error(0x0001, 'AgentExample', `err: ${JSON.stringify(err)}, message: ${err.message}`);
},
controller: this.controller
})
}
} Tap the button to open the agent pop-up.
import { BusinessError } from '@kit.BasicServicesKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import {
FunctionComponent,
FunctionController
} from '@kit.AgentFrameworkKit';
@Entry
@Component
export struct AgentExample {
private controller: FunctionController = new FunctionController();
private agentId: string = 'agentproxy65481da1fa2293a8482d45';
aboutToAppear() {
this.initListeners();
}
initListeners() {
this.controller?.on('agentDialogOpened', this.onAgentOpenedCallback);
this.controller?.on('agentDialogClosed', this.onAgentClosedCallback);
}
onAgentOpenedCallback = () => {
hilog.info(0x0001, 'AgentExample', 'agent dialog opened callback');
};
onAgentClosedCallback = () => {
hilog.info(0x0001, 'AgentExample', 'agent dialog closed callback');
};
aboutToDisappear() {
this.controller?.off('agentDialogOpened');
this.controller?.off('agentDialogClosed');
}
build() {
Column() {
FunctionComponent({
agentId: this.agentId,
onError: (err: BusinessError) => {
hilog.error(0x0001, 'AgentExample', `err: ${JSON.stringify(err)}, message: ${err.message}`);
},
options: {
title: 'Intelligent creation',
queryText: 'Create a scenario',
isShowShadow: true
},
controller: this.controller
})
}
}
}