文档管理中心
您当前正在浏览HarmonyOS最新文档,覆盖已发布的所有API版本,可在API参考中筛选您使用的API版本。详细的版本配套关系请参考版本说明
开发与测试开放能力APIAIAgent Framework Kit(智能体框架服务)ArkTS组件FunctionComponent(功能组件)

FunctionComponent(功能组件)

Phone6.0.0(20)+Tablet6.0.0(20)+

Agent Framework Kit(智能体框架服务)提供了拉起指定智能体的能力。

应用在小艺开放平台上线智能体后,向用户提供应用+智能体组合的服务,让用户可以在适当的场景下通过Agent Framework Kit的UI控件能力主动拉起智能体。配置智能体请参考开发Agent

起始版本: 6.0.0(20)

导入模块

收起
自动换行
深色代码主题
复制
  1. import { AgentController, BaseOptions, FunctionController, FunctionOptions, ButtonType, FunctionComponent } from '@kit.AgentFrameworkKit';

FunctionComponent

Phone6.0.0(20)+Tablet6.0.0(20)+

Agent功能组件,可以实现拉起智能体功能。

装饰器类型: @Component

元服务API: 从版本6.0.1(21)开始,该接口支持在元服务中使用。

系统能力: SystemCapability.AI.Agent.AgentKit

模型约束: 此接口仅可在Stage模型下使用。

起始版本: 6.0.0(20)

参数:

展开
名称 类型 只读 必填 装饰器类型 说明
agentId string -

Agent ID,Agent的唯一标识,开发Agent时获取。智能体创建成功后,可在智能体配置页面的网址链接中获取。

长度限制:1~64个字符。

onError ErrorCallback - 错误回调函数。返回的错误码及说明如下所示
options FunctionOptions - Function组件初始化可选参数。
controller FunctionController - Function组件控制器。默认为undefined。
说明

该组件的所有参数均为初始化配置项,仅在组件实例化时生效,运行过程中修改参数值无法实现动态更新,若需调整参数请重新初始化组件。

错误码:

以下错误码为onError回调函数返回的,详细介绍请参见ArkTS API错误码

展开
错误码ID 错误信息
1022400010 Parameter error.
1022400014 Internal error.

build

Phone6.0.0(20)+Tablet6.0.0(20)+

build(): void

FunctionComponent组件的构造函数。

元服务API: 从版本6.0.1(21)开始,该接口支持在元服务中使用。

系统能力: SystemCapability.AI.Agent.AgentKit

模型约束: 此接口仅可在Stage模型下使用。

起始版本: 6.0.0(20)

示例:

收起
自动换行
深色代码主题
复制
  1. import { BusinessError } from "@kit.BasicServicesKit";
  2. import { hilog } from "@kit.PerformanceAnalysisKit";
  3. import { FunctionComponent } from '@kit.AgentFrameworkKit';
  4. @Entry
  5. @Component
  6. struct FunctionExample {
  7. private agentId: string = 'agentproxy65481da1fa2293a8482d45'; // 智能体对应的agentId,由小艺智能体平台在创建智能体时指定
  8. build() {
  9. Column() {
  10. FunctionComponent({
  11. agentId: this.agentId,
  12. onError: (err: BusinessError) => {
  13. hilog.error(0x0001, 'FunctionExample', `err code: ${err.code}, message: ${err.message}`);
  14. },
  15. options: {
  16. title: '智能创建',
  17. queryText: '创建一个新的模式',
  18. isShowShadow: true
  19. }
  20. })
  21. }
  22. }
  23. }

AgentController

Phone6.0.0(20)+Tablet6.0.0(20)+

Agent Framework Kit控件通用控制器。集成了开发者与Agent功能组件交互的通用方法,该类为控制器基类,可被其它更高阶的控制器继承。

元服务API: 从版本6.0.1(21)开始,该接口支持在元服务中使用。

系统能力: SystemCapability.AI.Agent.AgentKit

模型约束: 此接口仅可在Stage模型下使用。

起始版本: 6.0.0(20)

isAgentSupport

Phone6.0.0(20)+Tablet6.0.0(20)+

isAgentSupport(context: common.UIAbilityContext, agentId: string): Promise<boolean>

查询Agent是否可用。若可用,返回Agent信息。使用Promise异步回调。

元服务API: 从版本6.0.1(21)开始,该接口支持在元服务中使用。

系统能力: SystemCapability.AI.Agent.AgentKit

模型约束: 此接口仅可在Stage模型下使用。

起始版本: 6.0.0(20)

参数:

展开
参数名 类型 必填 说明
context common.UIAbilityContext 当前上下文环境。
agentId string

Agent ID,Agent的唯一标识,开发Agent时获取。

长度限制:1~64个字符。

返回值:

展开
类型 说明
Promise<boolean> Promise对象,返回true表示agentId有效且Agent功能支持,返回false表示agentId无效或者Agent功能不支持。

错误码:

以下错误码的详细介绍请参见ArkTS API错误码

展开
错误码ID 错误信息
1022400010 Parameter error.
1022400011 Privacy agreement not accepted.
1022400012 HUAWEI ID not signed in.
1022400013 Internet error.
1022400014 Internal error.

示例:

收起
自动换行
深色代码主题
复制
  1. import { BusinessError } from "@kit.BasicServicesKit";
  2. import { hilog } from "@kit.PerformanceAnalysisKit";
  3. import { common } from "@kit.AbilityKit";
  4. import { FunctionComponent, FunctionController } from '@kit.AgentFrameworkKit';
  5. @Entry
  6. @Component
  7. struct AgentDemo {
  8. private functionController: FunctionController = new FunctionController();
  9. private agentId: string = 'agentproxy65481da1fa2293a8482d45'; // 智能体对应的agentId,由小艺智能体平台在创建智能体时指定
  10. @State isAgentSupport: boolean = false;
  11. aboutToAppear() {
  12. void this.checkAgentSupport();
  13. }
  14. async checkAgentSupport() {
  15. try {
  16. let context = this.getUIContext()?.getHostContext() as common.UIAbilityContext;
  17. this.isAgentSupport = await this.functionController.isAgentSupport(context, this.agentId);
  18. } catch (err) {
  19. hilog.error(0x0001, 'AgentExample', `err code: ${err.code}, message: ${err.message}`);
  20. }
  21. }
  22. build() {
  23. Column() {
  24. // 若使用该方法判断是否加载,请确保agentId可用
  25. if (this.isAgentSupport) {
  26. FunctionComponent({
  27. agentId: this.agentId,
  28. onError: (err: BusinessError) => {
  29. hilog.error(0x0001, 'AgentExample', `err: ${JSON.stringify(err)}, message: ${err.message}`);
  30. },
  31. options: {
  32. title: '智能创建',
  33. queryText: '创建一个新的模式',
  34. isShowShadow: true
  35. }
  36. })
  37. }
  38. }
  39. }
  40. }

on('agentDialogOpened')

Phone6.0.0(20)+Tablet6.0.0(20)+

on(type: 'agentDialogOpened', callback: Callback<void>): void

监听智能体对话框打开事件。使用callback异步回调。

元服务API: 从版本6.0.1(21)开始,该接口支持在元服务中使用。

系统能力: SystemCapability.AI.Agent.AgentKit

模型约束: 此接口仅可在Stage模型下使用。

起始版本: 6.0.0(20)

参数:

展开
参数名 类型 必填 说明
type string 值为'agentDialogOpened',监听智能体对话框打开事件。
callback Callback<void> 回调函数。当具体的操作成功,err为undefined,否则为错误对象。

示例:

收起
自动换行
深色代码主题
复制
  1. import { BusinessError } from "@kit.BasicServicesKit";
  2. import { hilog } from "@kit.PerformanceAnalysisKit";
  3. import { FunctionComponent, FunctionController } from '@kit.AgentFrameworkKit';
  4. @Entry
  5. @Component
  6. struct AgentDemo{
  7. private controller: FunctionController = new FunctionController();
  8. private agentId: string = 'agentproxy65481da1fa2293a8482d45'; // 智能体对应的agentId,由小艺智能体平台在创建智能体时指定
  9. aboutToAppear() {
  10. this.controller?.on('agentDialogOpened', this.onAgentOpenedCallback);
  11. this.controller?.on('agentDialogClosed', this.onAgentClosedCallback);
  12. }
  13. onAgentOpenedCallback = () => {
  14. hilog.info(0x0001, 'AgentExample', 'agent dialog opened callback');
  15. };
  16. onAgentClosedCallback = () => {
  17. hilog.info(0x0001, 'AgentExample', 'agent dialog closed callback');
  18. };
  19. aboutToDisappear() {
  20. this.controller?.off('agentDialogOpened');
  21. this.controller?.off('agentDialogClosed');
  22. }
  23. build() {
  24. Column() {
  25. FunctionComponent({
  26. agentId: this.agentId,
  27. onError: (err: BusinessError) => {
  28. hilog.error(0x0001, 'AgentExample', `err: ${JSON.stringify(err)}, message: ${err.message}`);
  29. },
  30. options: {
  31. title: '智能创建',
  32. queryText: '创建一个新的模式',
  33. isShowShadow: true
  34. },
  35. controller: this.controller
  36. })
  37. }
  38. }
  39. }

off('agentDialogOpened')

Phone6.0.0(20)+Tablet6.0.0(20)+

off(type: 'agentDialogOpened', callback?: Callback<void>): void

取消智能体对话框打开事件的监听。使用callback异步回调。

元服务API: 从版本6.0.1(21)开始,该接口支持在元服务中使用。

系统能力: SystemCapability.AI.Agent.AgentKit

模型约束: 此接口仅可在Stage模型下使用。

起始版本: 6.0.0(20)

参数:

展开
参数名 类型 必填 说明
type string 值为 'agentDialogOpened',取消智能体对话框打开事件的监听。
callback Callback<void> 回调函数,需要取消注册的回调函数,需与订阅时传入的回调函数是同一个。若无此参数,则取消注册所有的回调函数。当具体的操作成功,err为undefined,否则为错误对象。

示例:

收起
自动换行
深色代码主题
复制
  1. import { BusinessError } from "@kit.BasicServicesKit";
  2. import { hilog } from "@kit.PerformanceAnalysisKit";
  3. import { FunctionComponent, FunctionController } from '@kit.AgentFrameworkKit';
  4. @Entry
  5. @Component
  6. struct AgentDemo{
  7. private controller: FunctionController = new FunctionController();
  8. private agentId: string = 'agentproxy65481da1fa2293a8482d45'; // 智能体对应的agentId,由小艺智能体平台在创建智能体时指定
  9. aboutToAppear() {
  10. this.controller?.on('agentDialogOpened', this.onAgentOpenedCallback);
  11. this.controller?.on('agentDialogClosed', this.onAgentClosedCallback);
  12. }
  13. onAgentOpenedCallback = () => {
  14. hilog.info(0x0001, 'AgentExample', 'agent dialog opened callback');
  15. };
  16. onAgentClosedCallback = () => {
  17. hilog.info(0x0001, 'AgentExample', 'agent dialog closed callback');
  18. };
  19. aboutToDisappear() {
  20. this.controller?.off('agentDialogOpened');
  21. this.controller?.off('agentDialogClosed');
  22. }
  23. build() {
  24. Column() {
  25. FunctionComponent({
  26. agentId: this.agentId,
  27. onError: (err: BusinessError) => {
  28. hilog.error(0x0001, 'AgentExample', `err: ${JSON.stringify(err)}, message: ${err.message}`);
  29. },
  30. options: {
  31. title: '智能创建',
  32. queryText: '创建一个新的模式',
  33. isShowShadow: true
  34. },
  35. controller: this.controller
  36. })
  37. }
  38. }
  39. }

on('agentDialogClosed')

Phone6.0.0(20)+Tablet6.0.0(20)+

on(type: 'agentDialogClosed', callback: Callback<void>): void

监听智能体对话框关闭事件。使用callback异步回调。

元服务API: 从版本6.0.1(21)开始,该接口支持在元服务中使用。

系统能力: SystemCapability.AI.Agent.AgentKit

模型约束: 此接口仅可在Stage模型下使用。

起始版本: 6.0.0(20)

参数:

展开
参数名 类型 必填 说明
type string 值为'agentDialogClosed',监听智能体对话框关闭事件。
callback Callback<void> 回调函数。当具体的操作成功,err为undefined,否则为错误对象。

示例:

收起
自动换行
深色代码主题
复制
  1. import { BusinessError } from "@kit.BasicServicesKit";
  2. import { hilog } from "@kit.PerformanceAnalysisKit";
  3. import { FunctionComponent, FunctionController } from '@kit.AgentFrameworkKit';
  4. @Entry
  5. @Component
  6. struct AgentDemo{
  7. private controller: FunctionController = new FunctionController();
  8. private agentId: string = 'agentproxy65481da1fa2293a8482d45'; // 智能体对应的agentId,由小艺智能体平台在创建智能体时指定
  9. aboutToAppear() {
  10. this.controller?.on('agentDialogOpened', this.onAgentOpenedCallback);
  11. this.controller?.on('agentDialogClosed', this.onAgentClosedCallback);
  12. }
  13. onAgentOpenedCallback = () => {
  14. hilog.info(0x0001, 'AgentExample', 'agent dialog opened callback');
  15. };
  16. onAgentClosedCallback = () => {
  17. hilog.info(0x0001, 'AgentExample', 'agent dialog closed callback');
  18. };
  19. aboutToDisappear() {
  20. this.controller?.off('agentDialogOpened');
  21. this.controller?.off('agentDialogClosed');
  22. }
  23. build() {
  24. Column() {
  25. FunctionComponent({
  26. agentId: this.agentId,
  27. onError: (err: BusinessError) => {
  28. hilog.error(0x0001, 'AgentExample', `err: ${JSON.stringify(err)}, message: ${err.message}`);
  29. },
  30. options: {
  31. title: '智能创建',
  32. queryText: '创建一个新的模式',
  33. isShowShadow: true
  34. },
  35. controller: this.controller
  36. })
  37. }
  38. }
  39. }

off('agentDialogClosed')

Phone6.0.0(20)+Tablet6.0.0(20)+

off(type: 'agentDialogClosed', callback?: Callback<void>): void

取消智能体对话框关闭事件的监听。使用callback异步回调。

元服务API: 从版本6.0.1(21)开始,该接口支持在元服务中使用。

系统能力: SystemCapability.AI.Agent.AgentKit

模型约束: 此接口仅可在Stage模型下使用。

起始版本: 6.0.0(20)

参数:

展开
参数名 类型 必填 说明
type string 值为 'agentDialogClosed',取消智能体对话框关闭事件的监听。
callback Callback<void> 回调函数,需要取消注册的回调函数,需与订阅时传入的回调函数是同一个。若不传入此参数,则取消注册所有的回调函数。当具体的操作成功,err为undefined,否则为错误对象。

示例:

收起
自动换行
深色代码主题
复制
  1. import { BusinessError } from "@kit.BasicServicesKit";
  2. import { hilog } from "@kit.PerformanceAnalysisKit";
  3. import { FunctionComponent, FunctionController } from '@kit.AgentFrameworkKit';
  4. @Entry
  5. @Component
  6. struct AgentDemo{
  7. private controller: FunctionController = new FunctionController();
  8. private agentId: string = 'agentproxy65481da1fa2293a8482d45'; // 智能体对应的agentId,由小艺智能体平台在创建智能体时指定
  9. aboutToAppear() {
  10. this.controller?.on('agentDialogOpened', this.onAgentOpenedCallback);
  11. this.controller?.on('agentDialogClosed', this.onAgentClosedCallback);
  12. }
  13. onAgentOpenedCallback = () => {
  14. hilog.info(0x0001, 'AgentExample', 'agent dialog opened callback');
  15. };
  16. onAgentClosedCallback = () => {
  17. hilog.info(0x0001, 'AgentExample', 'agent dialog closed callback');
  18. };
  19. aboutToDisappear() {
  20. this.controller?.off('agentDialogOpened');
  21. this.controller?.off('agentDialogClosed');
  22. }
  23. build() {
  24. Column() {
  25. FunctionComponent({
  26. agentId: this.agentId,
  27. onError: (err: BusinessError) => {
  28. hilog.error(0x0001, 'AgentExample', `err: ${JSON.stringify(err)}, message: ${err.message}`);
  29. },
  30. options: {
  31. title: '智能创建',
  32. queryText: '创建一个新的模式',
  33. isShowShadow: true
  34. },
  35. controller: this.controller
  36. })
  37. }
  38. }
  39. }

FunctionController

Phone6.0.0(20)+Tablet6.0.0(20)+

Function组件控制器,用于与Function组件控制交互。预留接口,当前版本完全继承AgentController的方法,无额外的实现。

元服务API: 从版本6.0.1(21)开始,该接口支持在元服务中使用。

系统能力: SystemCapability.AI.Agent.AgentKit

模型约束: 此接口仅可在Stage模型下使用。

起始版本: 6.0.0(20)

BaseOptions

Phone6.0.0(20)+Tablet6.0.0(20)+

Agent组件可选参数的基础对象。集成了Agent组件的共用参数,用于开发者自定义组件属性,可被其它高阶参数继承。

元服务API: 从版本6.0.1(21)开始,该接口支持在元服务中使用。

系统能力: SystemCapability.AI.Agent.AgentKit

模型约束: 此接口仅可在Stage模型下使用。

起始版本: 6.0.0(20)

参数:

展开
参数名 类型 只读 可选 说明
title string

表示AgentFramework组件的标题。默认值为空。

FunctionOptions.buttonType按钮类型为ButtonType.CIRCLE时不体现。

当前只显示指定大小,宽度超过8个中文字符会进行省略。

字体大于1.75倍时默认最大显示两行。

titleFontSize number 表示AgentFramework组件标题的字体大小。取值范围为[14, 16]vp,默认值为16vp。取值在范围外取默认值。
iconSize number

表示AgentFramework组件图标的大小。取值范围为[16, 24]vp。

- 按钮类型为CIRCLE时,默认值为24vp。

- 按钮类型为CAPSULE时,默认值为20vp。

取值在范围外取默认值。

iconColors ResourceColor[] 表示图标颜色,当前仅支持设置一种颜色,不设置时,为默认渐变色图标。

FunctionOptions

Phone6.0.0(20)+Tablet6.0.0(20)+

FunctionComponent功能组件的初始化参数,继承自BaseOptions,开发者可通过该参数自定义FunctionComponent的属性。

元服务API: 从版本6.0.1(21)开始,该接口支持在元服务中使用。

系统能力: SystemCapability.AI.Agent.AgentKit

模型约束: 此接口仅可在Stage模型下使用。

起始版本: 6.0.0(20)

参数:

展开
参数名 类型 只读 可选 说明
queryText string 使用功能组件的初始查询文本,默认值为空。
controlSize ControlSize 功能组件按钮的大小,默认值为ControlSize.NORMAL。
buttonType ButtonType 功能组件的按钮类型。title属性为空或不传时默认值为ButtonType.CIRCLE,title属性不为空时默认值为ButtonType.CAPSULE
isShowShadow boolean

如果显示按钮的阴影,仅在 ButtonType.CAPSULE 胶囊类型时有效。默认值为false。

true:显示阴影。

false:不显示阴影。

backgroundColor ResourceColor

显示背景板时,组件的背景颜色。

默认值为sys.color.comp_background_tertiary背景色。

titleColors ResourceColor[]

功能组件带文本时,文本的颜色,当前最多仅支持设置两种颜色。

- 设置两种颜色时为渐变色。

- 设置一种颜色时为设置颜色。

- 不设置时为默认渐变色文本。

ButtonType

Phone6.0.0(20)+Tablet6.0.0(20)+

按钮类型的枚举值。

元服务API: 从版本6.0.1(21)开始,该接口支持在元服务中使用。

系统能力: SystemCapability.AI.Agent.AgentKit

模型约束: 此接口仅可在Stage模型下使用。

起始版本: 6.0.0(20)

展开
名称 说明
CIRCLE 0 圆形按钮。
CAPSULE 1

胶囊按钮(圆角默认高度为一半)。

说明:

字体倍数小于1.75倍时,显示为胶囊按钮,大于或等于1.75倍时,显示为矩形倒角按钮。

ICON_ABOVE_TITLE 2

图标与标题为上下结构的胶囊按钮。

起始版本: 6.0.1(21)

在 开发与测试 开放能力API 中进行搜索
请输入您想要搜索的关键词