尧图建网站 尧图建网站 YAOTU WEB BUILD 免费咨询
ARTICLE DETAIL

资讯详情

深耕网站建设与建站编程的一线实战洞察。

鸿蒙 7.0 智能体框架 2.0:意图即服务根因——从 InsightIntentExecutor 到 HMAF 2.0 Skill 演进

鸿蒙 7.0 智能体框架 2.0:意图即服务根因——从 InsightIntentExecutor 到 HMAF 2.0 Skill 演进 本文是「鸿蒙 7.0 新特性」系列第 3 篇。上篇讲 Component3D 空间计算一行加载 glTF 3D 模型真机渲染。本文讲智能体框架核心意图即服务——根因在意图路由表。鸿蒙 7.0API 26发布 HMAF 2.0鸿蒙智能体框架 2.0本文用本机 API 21 就能跑的 InsightIntentExecutorAPI 11真机演示再讲鸿蒙 7.0 智能体框架演进根因InsightIntentExecutorAPI 11→ HMAF 2.0 SkillAPI 26。一、开篇意图即服务不是 if-else 路由是意图路由表绑定你写 React 时意图分发是「if-else 魔法」条件判断路由到不同 handler// React 意图分发if-else 魔法条件判断路由functiondispatchIntent(intent:string,params:Recordstring,any):string{if(intentquery_weather){// ✅ if-else 路由returnqueryWeather(params.location)}elseif(intentplay_music){returnplayMusic(params.song)}elseif(intentset_alarm){returnsetAlarm(params.time)}return未知意图}// React 意图分发if-else 魔法硬编码路由无框架托管你写鸿蒙 ArkTS 时意图即服务是意图路由表绑定——InsightIntentExecutor 继承 onExecute 路由// ArkTS 意图即服务InsightIntentExecutor 意图路由表importInsightIntentExecutorfromohos.app.ability.InsightIntentExecutorimportinsightIntentfromohos.app.ability.insightIntentimport{window}fromkit.ArkUI// ✅ 自定义意图执行器继承 InsightIntentExecutorclassWeatherIntentExecutorextendsInsightIntentExecutor{// ✅ 培景模式执行UIAbility 培台onExecuteInUIAbilityForegroundMode(name:string,param:Recordstring,Object,pageLoader:window.WindowStage):insightIntent.ExecuteResult|PromiseinsightIntent.ExecuteResult{constresult:Recordstring,Object{}result[message]this.routeIntent(name,param)return{code:0,result:result}asinsightIntent.ExecuteResult}// ✅ 核心意图路由表意图名 → Skill 执行routeIntent(name:string,param:Recordstring,Object):string{if(namequery_weather){constlocation(param[location]asstring)||北京returnSkill[query_weather] 执行查询${location}天气}elseif(nameplay_music){returnSkill[play_music] 执行播放${param[song]}}return未知意图${name}}}// ArkTS 意图即服务InsightIntentExecutor 意图路由表框架托管培景/后台模式分发if-else 魔法 vs 意图路由表绑定的区别React 把意图分发当 if-else 魔法硬编码路由无框架托管ArkTS 把 InsightIntentExecutor 当「意图路由表绑定」框架托管培景/后台模式分发。根因不是 if-else 魔法是意图路由表绑定——InsightIntentExecutor 意图路由表绑定框架托管培景/后台模式分发。二、根因InsightIntentExecutor 的意图路由表机制鸿蒙 ArkUI 的 InsightIntentExecutor 是意图路由表绑定——onExecute 回调 培景/后台模式分发来自三重绑定机制。机制 1InsightIntentExecutor 继承——框架托管意图执行InsightIntentExecutor 继承——框架托管意图执行培景/后台模式自动分发// ✅ InsightIntentExecutor 继承框架托管意图执行classWeatherIntentExecutorextendsInsightIntentExecutor{// ✅ 培景模式执行UIAbility 培台有 UIonExecuteInUIAbilityForegroundMode(name:string,param:Recordstring,Object,pageLoader:window.WindowStage):insightIntent.ExecuteResult|PromiseinsightIntent.ExecuteResult{constresult:Recordstring,Object{}result[message]this.routeIntent(name,param)return{code:0,result:result}asinsightIntent.ExecuteResult}// ✅ 后台模式执行无 UI后台静默onExecuteInUIAbilityBackgroundMode(name:string,param:Recordstring,Object):insightIntent.ExecuteResult|PromiseinsightIntent.ExecuteResult{constresult:Recordstring,Object{}result[message]this.routeIntent(name,param)return{code:0,result:result}asinsightIntent.ExecuteResult}}// InsightIntentExecutor 继承框架托管意图执行培景/后台模式自动分发InsightIntentExecutor 继承 vs 硬编码调用的区别InsightIntentExecutor 继承框架托管意图执行培景/后台模式自动分发硬编码调用手动调用 handler无框架托管。根因不是硬编码调用是 InsightIntentExecutor 继承——InsightIntentExecutor 继承框架托管意图执行培景/后台模式自动分发。机制 2意图路由表——意图名 → Skill 执行意图路由表——意图名 → Skill 执行根据 name 分发到不同 Skill// ✅ 意图路由表意图名 → Skill 执行routeIntent(name:string,param:Recordstring,Object):string{// ✅ 意图路由表意图名 → Skill 执行if(namequery_weather){constlocation(param[location]asstring)||北京returnSkill[query_weather] 执行查询${location}天气// ✅ 路由到 query_weather Skill}elseif(nameplay_music){constsong(param[song]asstring)||默认歌returnSkill[play_music] 执行播放${song}// ✅ 路由到 play_music Skill}elseif(nameset_alarm){consttime(param[time]asstring)||07:00returnSkill[set_alarm] 执行设置${time}闹钟// ✅ 路由到 set_alarm Skill}return未知意图${name}}// 意图路由表意图名 → Skill 执行根据 name 分发到不同 Skill意图路由表 vs if-else 硬编码的区别意图路由表意图名 → Skill 执行框架托管路由if-else 硬编码条件判断路由无框架托管。根因不是 if-else 硬编码是意图路由表——意图路由表意图名 → Skill 执行框架托管路由。机制 3ExecuteResult 标准返回——code result 双字段ExecuteResult 标准返回——code结果码 result执行结果双字段// ✅ ExecuteResult 标准返回code result 双字段onExecuteInUIAbilityForegroundMode(name:string,param:Recordstring,Object,pageLoader:window.WindowStage):insightIntent.ExecuteResult|PromiseinsightIntent.ExecuteResult{constresult:Recordstring,Object{}result[message]this.routeIntent(name,param)// ✅ ExecuteResultcode结果码 0成功 result执行结果 Recordreturn{code:0,result:result}asinsightIntent.ExecuteResult}// ExecuteResult 标准返回code结果码 result执行结果 Record双字段ExecuteResult 标准返回 vs 自定义返回的区别ExecuteResult 标准返回code result 双字段框架规范自定义返回任意结构无规范。根因不是自定义返回是 ExecuteResult 标准返回——ExecuteResult 标准返回 code result 双字段框架规范。三、真机配图鸿蒙智能体框架 2.0——意图即服务真机配图展示鸿蒙智能体框架 2.0 意图即服务初始态意图执行器注册就绪显示 InsightIntentExecutor 注册区 Skill 意图路由区query_weather/play_music/set_alarm 三个 Skill 路由表 四个触发按钮意图路由执行态点击「查天气北京」按钮意图路由 query_weather → Skill[query_weather] 执行意图日志显示namequery_weather, param{location:北京}执行结果显示Skill[query_weather] 执行查询 北京 天气多意图切换态点击「查天气上海」按钮意图路由切换到上海执行结果显示Skill[query_weather] 执行查询 上海 天气验证意图路由表多意图分发四、真解法鸿蒙智能体框架 2.0 的三个场景场景 1InsightIntentExecutor 意图路由90% 场景首选意图即服务InsightIntentExecutor 意图路由用继承 onExecute routeIntent// ✅ 场景 1InsightIntentExecutor 意图路由意图即服务classWeatherIntentExecutorextendsInsightIntentExecutor{onExecuteInUIAbilityForegroundMode(name:string,param:Recordstring,Object,pageLoader:window.WindowStage):insightIntent.ExecuteResult|PromiseinsightIntent.ExecuteResult{constresult:Recordstring,Object{}result[message]this.routeIntent(name,param)return{code:0,result:result}asinsightIntent.ExecuteResult}routeIntent(name:string,param:Recordstring,Object):string{if(namequery_weather){returnSkill[query_weather] 执行查询${param[location]}天气}return未知意图${name}}}// InsightIntentExecutor 意图路由继承 onExecute routeIntent意图即服务场景 2培景/后台模式分发UIAbility 培台 vs 后台静默培景/后台模式分发——onExecuteInUIAbilityForegroundMode培台有 UIvs onExecuteInUIAbilityBackgroundMode后台静默// ✅ 场景 2培景/后台模式分发UIAbility 培台 vs 后台静默classWeatherIntentExecutorextendsInsightIntentExecutor{// ✅ 培景模式执行UIAbility 培台有 UIonExecuteInUIAbilityForegroundMode(name:string,param:Recordstring,Object,pageLoader:window.WindowStage):insightIntent.ExecuteResult|PromiseinsightIntent.ExecuteResult{constresult:Recordstring,Object{}result[message]this.routeIntent(name,param)return{code:0,result:result}asinsightIntent.ExecuteResult}// ✅ 后台模式执行无 UI后台静默onExecuteInUIAbilityBackgroundMode(name:string,param:Recordstring,Object):insightIntent.ExecuteResult|PromiseinsightIntent.ExecuteResult{constresult:Recordstring,Object{}result[message]this.routeIntent(name,param)return{code:0,result:result}asinsightIntent.ExecuteResult}}// 培景/后台模式分发培景有 UI vs 后台静默框架自动分发场景 3鸿蒙 7.0 HMAF 2.0 SkillAPI 26 describe execute鸿蒙 7.0API 26升级 HMAF 2.0——kit.AgentKit Skill describe execute// ✅ 场景 3鸿蒙 7.0 HMAF 2.0 SkillAPI 26 describe execute// ⚠ 鸿蒙 7.0API 26新特性本机 API 21 降级为 InsightIntentExecutor// ✅ 鸿蒙 7.0 HMAF 2.0 SkillAPI 26// import { Skill, SkillContext, SkillResult, SkillParameter } from kit.AgentKit// export class WeatherQuerySkill extends Skill {// describe(): SkillParameter { // ✅ describe 声明能力// return {// name: weather_query,// description: 查询指定城市的实时天气信息,// parameters: {// location: { type: string, description: 城市名称, required: true }// }// }// }// async execute(context: SkillContext): PromiseSkillResult { // ✅ execute 执行逻辑// const location context.params.location as string// return SkillResult.success({ location, temperature: 25, condition: 晴 })// }// }// 鸿蒙 7.0 HMAF 2.0Skill describe声明能力 execute执行逻辑意图即服务// ✅ 降级方案API 21 用 InsightIntentExecutor// class WeatherIntentExecutor extends InsightIntentExecutor { ... }鸿蒙 7.0 HMAF 2.0 Skill vs InsightIntentExecutor 的区别鸿蒙 7.0 HMAF 2.0 SkillAPI 26kit.AgentKitdescribe 声明能力 execute 执行逻辑InsightIntentExecutorAPI 11继承 onExecute 路由。根因不是 InsightIntentExecutor 是鸿蒙 7.0 HMAF 2.0 Skill——鸿蒙 7.0 HMAF 2.0 Skill describe execute意图即服务。五、一句话哲学写鸿蒙 ArkUI 记住意图即服务不是 if-else 路由是意图路由表绑定——InsightIntentExecutor 继承框架托管意图执行。根因不是 if-else 魔法是意图路由表绑定——InsightIntentExecutor 继承框架托管培景/后台模式自动分发 意图路由表意图名 → Skill 执行框架托管路由 ExecuteResult 标准返回code result 双字段框架规范 鸿蒙 7.0 HMAF 2.0 SkillAPI 26kit.AgentKitdescribe 声明能力 execute 执行逻辑。InsightIntentExecutor 意图路由用继承 onExecute routeIntent首选90% 场景培景/后台模式分发用 onExecuteInUIAbilityForegroundMode vs onExecuteInUIAbilityBackgroundMode鸿蒙 7.0 HMAF 2.0 Skill 升级API 26describe execute意图即服务。意图路由表绑定框架托管培景/后台模式分发是鸿蒙 7.0 智能体框架 2.0 核心
返回列表