has.modelContext.getContext(): ModelContext
获取AI半屏通信上下文ModelContext对象。
起始版本: 2.0.5
依赖关系: HarmonyOS SDK版本≥6.0.0(20)且ROM版本≥6.0.0
返回值:
返回ModelContext对象。
示例:
const modelContext = has.modelContext.getContext();
ModelContext实例。可通过has.modelContext.getContext接口获取实例。
起始版本: 2.0.5
依赖关系: HarmonyOS SDK版本≥6.0.0(20)且ROM版本≥6.0.0
约束与限制:
ModelContext.sendFollowUpMessage(Object object)
向AI宿主发送追加消息,由模型决定后续行为。发送完成后会自动关闭半屏页面。
起始版本: 2.0.5
依赖关系: HarmonyOS SDK版本≥6.0.0(20)且ROM版本≥6.0.0
参数:
参数为Object对象,包括以下字段。
| 参数 | 类型 | 必填 | 描述 |
|---|---|---|---|
| content | Array<MessageItem> | 是 | 消息内容列表,必须包含至少一个type='text'的项。 |
| success | function | 否 | 接口调用成功的回调函数。 |
| fail | function | 否 | 接口调用失败的回调函数。 |
| complete | function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行)。 |
MessageItem:
消息项,支持文本和原子接口调用两种类型。
| 取值 | 说明 |
|---|---|
| { type: 'text', text: string } | 文本消息项,text为文本内容。 |
| { type: 'api/call', data: { name: string, arguments: Object } } | 原子接口调用消息项,name为原子接口名称,arguments为原子接口调用参数。 |
错误码信息:
| 错误码 | 错误信息 |
|---|---|
| 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. |
示例:
const modelContext = has.modelContext.getContext();
modelContext.sendFollowUpMessage({
content: [
{ type: 'text', text: '用户选择了商品A' }
],
success: () => {
console.info('sendFollowUpMessage success');
},
fail: (err) => {
console.error('sendFollowUpMessage fail', err);
},
complete: (res) => {
console.info('sendFollowUpMessage complete', res);
}
}); ModelContext.reapplyApiCall(Object object)
将原子接口调用信息回传给AI宿主执行。回传完成后会自动关闭半屏页面。
起始版本: 2.0.5
依赖关系: HarmonyOS SDK版本≥6.0.0(20)且ROM版本≥6.0.0
参数:
参数为Object对象,包括以下字段。
| 参数 | 类型 | 必填 | 描述 |
|---|---|---|---|
| arguments | ReapplyApiCallArguments | 是 | 回传的原子接口信息,name不能为空。 |
| success | function | 否 | 接口调用成功的回调函数。 |
| fail | function | 否 | 接口调用失败的回调函数。 |
| complete | function | 否 | 接口调用结束的回调函数(调用成功、失败都会执行)。 |
ReapplyApiCallArguments:
回传的原子接口信息。
| 参数 | 类型 | 必填 | 描述 |
|---|---|---|---|
| name | string | 是 | 原子接口名称,不能为空。 |
| arguments | Object | 是 | 原子接口调用参数。 |
错误码信息:
| 错误码 | 错误信息 |
|---|---|
| 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. |
示例:
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);
}
});