目录

AI Half-Screen Communication

has.modelContext.getContext

has.modelContext.getContext(): ModelContext

Obtains a ModelContext object of AI half-screen communication.

Since: 2.0.5

Dependency: HarmonyOS SDK version 6.0.0(20) or later and ROM version 6.0.0 or later

Returns:

ModelContext object.

Example:

const modelContext = has.modelContext.getContext();

ModelContext

ModelContext instance, which can be obtained through has.modelContext.getContext.

Since: 2.0.5

Dependency: HarmonyOS SDK version 6.0.0(20) or later and ROM version 6.0.0 or later

Constraints:

[h2]ModelContext.sendFollowUpMessage

ModelContext.sendFollowUpMessage(Object object)

Sends an additional message to the AI host. The model determines the subsequent behavior. After the message is sent, the half-screen page is automatically closed.

Since: 2.0.5

Dependency: HarmonyOS SDK version 6.0.0(20) or later and ROM version 6.0.0 or later

Parameters

The input parameter is an object containing the following fields.

Parameter Type Mandatory Description
content Array<MessageItem> Yes Message content list, which must contain at least one item whose type is text.
success function No Callback triggered when the API call is successful.
fail function No Callback triggered when the API call fails.
complete function No Callback triggered when the API call is complete, regardless of success or failure.

MessageItem

Message item, which can be a text or an atomic API call.

Value Description
{ type: 'text', text: string } Text message item, where text indicates the text content.
{ type: 'api/call', data: { name: string, arguments: Object } } Atomic API call message item, where name indicates the atomic API name and arguments indicates the atomic API call parameters.

Error code information

Error Code Error Message
17200001 AtomicServiceAI not initialized.
17200002 callerBundleName not in whitelist.
17200003 content must not be empty / content must contain at least one text item.
17200005 Failed to resolve publish bundle name.
17200006 Failed to publish sendFollowUpMessage event.
17200007 API version 20 required.

Example:

const modelContext = has.modelContext.getContext();
modelContext.sendFollowUpMessage({
  content: [
    { type: 'text', text: 'The user has selected product A.' }
  ],
  success: () => {
    console.info('sendFollowUpMessage success');
  },
  fail: (err) => {
    console.error('sendFollowUpMessage fail', err);
  },
  complete: (res) => {
    console.info('sendFollowUpMessage complete', res);
  }
});

[h2]ModelContext.reapplyApiCall

ModelContext.reapplyApiCall(Object object)

Sends the atomic API call information back to the AI host for execution. After the information is sent back, the half-screen page is automatically closed.

Since: 2.0.5

Dependency: HarmonyOS SDK version 6.0.0(20) or later and ROM version 6.0.0 or later

Parameters

The input parameter is an object containing the following fields.

Parameter Type Mandatory Description
arguments ReapplyApiCallArguments Yes Returned atomic API information. The name cannot be empty.
success function No Callback triggered when the API call is successful.
fail function No Callback triggered when the API call fails.
complete function No Callback triggered when the API call is complete, regardless of success or failure.

ReapplyApiCallArguments

Returned atomic API information.

Parameter Type Mandatory Description
name string Yes Atomic API name, which cannot be empty.
arguments Object Yes Parameters called by the atomic API.

Error code information

Error Code Error Message
17200001 AtomicServiceAI not initialized.
17200002 callerBundleName not in whitelist.
17200004 reapplyApiCall arguments error.
17200005 Failed to resolve publish bundle name.
17200006 Failed to publish reapplyApiCall event.
17200007 API version 20 required.

Example:

const modelContext = has.modelContext.getContext();
modelContext.reapplyApiCall({
  arguments: {
    name: 'confirmOrder',
    arguments: { orderId: 'order_001', status: 'confirmed' }
  },
  success: () => {
    console.info('reapplyApiCall success');
  },
  fail: (err) => {
    console.error('reapplyApiCall fail', err);
  },
  complete: (res) => {
    console.info('reapplyApiCall complete', res);
  }
});