目录

Starting an Agent Through a Function Component

Use Cases

Preparations

Development Procedure

  1. 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';
  2. (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'
              }
            })
          }
        }
      }
  3. 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
          })
        }
      }
    }
  4. 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
          })
        }
      }

Development Example

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
      })
    }
  }
}