1 /*
2 * Copyright (c) 2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "extract_insight_intent_profile.h"
17
18 #include "hilog_tag_wrapper.h"
19 #include "json_util.h"
20
21 namespace OHOS {
22 namespace AbilityRuntime {
23 using JsonType = AppExecFwk::JsonType;
24 using ArrayType = AppExecFwk::ArrayType;
25
26 namespace {
27 int32_t g_extraParseResult = ERR_OK;
28 std::mutex g_extraMutex;
29
30 const std::string INSIGHT_INTENTS = "extractInsightIntents";
31 const std::string INSIGHT_INTENT_NAME = "intentName";
32 const std::string INSIGHT_INTENT_DOMAIN = "domain";
33 const std::string INSIGHT_INTENT_VERSION = "intentVersion";
34 const std::string INSIGHT_INTENT_EXECUTE_MODE = "executeMode";
35 const std::string INSIGHT_INTENT_DECORETOR_FILE = "decoratorFile";
36 const std::string INSIGHT_INTENT_DECORETOR_CLASS = "decoratorClass";
37 const std::string INSIGHT_INTENT_DECORETOR_TYPE = "decoratorType";
38 const std::string INSIGHT_INTENT_BUNDLE_NAME = "bundleName";
39 const std::string INSIGHT_INTENT_MODULE_NAME = "moduleName";
40 const std::string INSIGHT_INTENT_DISPLAY_NAME = "displayName";
41 const std::string INSIGHT_INTENT_DISPLAY_DESCRIPTION = "displayDescription";
42 const std::string INSIGHT_INTENT_SCHEMA = "schema";
43 const std::string INSIGHT_INTENT_ICON = "icon";
44 const std::string INSIGHT_INTENT_LLM_DESCRIPTION = "llmDescription";
45 const std::string INSIGHT_INTENT_KEYWORDS = "keywords";
46 const std::string INSIGHT_INTENT_PARAMETERS = "parameters";
47 const std::string INSIGHT_INTENT_URI = "uri";
48 const std::string INSIGHT_INTENT_PARAM_MAPPING = "paramMappings";
49 const std::string INSIGHT_INTENT_UI_ABILITY = "uiAbility";
50 const std::string INSIGHT_INTENT_PAGE_ROUTE_NAME = "pagePath";
51 const std::string INSIGHT_INTENT_NAVIGATION_ID = "navigationId";
52 const std::string INSIGHT_INTENT_NAV_DESTINATION_NAME = "navDestinationName";
53 const std::string INSIGHT_INTENT_ABILITY_NAME = "abilityName";
54 const std::string INSIGHT_INTENT_FUNCTION_NAME = "functionName";
55 const std::string INSIGHT_INTENT_FUNCTION_PARAMS = "functionParamList";
56 const std::string INSIGHT_INTENT_PARAM_NAME = "paramName";
57 const std::string INSIGHT_INTENT_PARAM_MAPPING_NAME = "paramMappingName";
58 const std::string INSIGHT_INTENT_PARAM_CATEGORY = "paramCategory";
59 const std::string INSIGHT_INTENT_RESULT = "result";
60 const std::string INSIGHT_INTENT_EXAMPLE = "example";
61 const std::string INSIGHT_INTENT_FORM_NAME = "formName";
62 const std::string INSIGHT_INTENT_ENTITES = "entities";
63 const std::string INSIGHT_INTENT_ENTITY_DECORETOR_FILE = "decoratorFile";
64 const std::string INSIGHT_INTENT_ENTITY_CLASS_NAME = "className";
65 const std::string INSIGHT_INTENT_ENTITY_DECORETOR_TYPE = "decoratorType";
66 const std::string INSIGHT_INTENT_ENTITY_ID = "entityId";
67 const std::string INSIGHT_INTENT_ENTITY_CATEGORY = "entityCategory";
68 const std::string INSIGHT_INTENT_ENTITY_PARENT_CLASS_NAME = "parentClassName";
69 const std::string INSIGHT_INTENT_ENTITY_PARAMETERS = "parameters";
70
71 enum DecoratorType {
72 DECORATOR_LINK = 0,
73 DECORATOR_PAGE,
74 DECORATOR_ENTRY,
75 DECORATOR_FUNCTION,
76 DECORATOR_FORM,
77 DECORATOR_UNKNOWN
78 };
79
StringToEnum(const std::string & input)80 DecoratorType StringToEnum(const std::string& input)
81 {
82 static const std::unordered_map<std::string, DecoratorType> mapping = {
83 {AbilityRuntime::INSIGHT_INTENTS_DECORATOR_TYPE_LINK, DecoratorType::DECORATOR_LINK},
84 {AbilityRuntime::INSIGHT_INTENTS_DECORATOR_TYPE_PAGE, DecoratorType::DECORATOR_PAGE},
85 {AbilityRuntime::INSIGHT_INTENTS_DECORATOR_TYPE_ENTRY, DecoratorType::DECORATOR_ENTRY},
86 {AbilityRuntime::INSIGHT_INTENTS_DECORATOR_TYPE_FUNCTION, DecoratorType::DECORATOR_FUNCTION},
87 {AbilityRuntime::INSIGHT_INTENTS_DECORATOR_TYPE_FORM, DecoratorType::DECORATOR_FORM}
88 };
89 auto it = mapping.find(input);
90 return (it != mapping.end()) ? it->second : DecoratorType::DECORATOR_UNKNOWN;
91 }
92
93 const std::map<std::string, ExecuteMode> executeModeMap = {
94 {"foreground", ExecuteMode::UI_ABILITY_FOREGROUND},
95 {"background", ExecuteMode::UI_ABILITY_BACKGROUND},
96 {"uiextension", ExecuteMode::UI_EXTENSION_ABILITY},
97 {"serviceextension", ExecuteMode::SERVICE_EXTENSION_ABILITY}
98 };
99 } // namespace
100
from_json(const nlohmann::json & jsonObject,LinkIntentParamProfileMapping & paramMapping)101 void from_json(const nlohmann::json &jsonObject, LinkIntentParamProfileMapping ¶mMapping)
102 {
103 TAG_LOGD(AAFwkTag::INTENT, "LinkIntentParamProfileMapping from json");
104 const auto &jsonObjectEnd = jsonObject.end();
105 AppExecFwk::BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
106 jsonObjectEnd,
107 INSIGHT_INTENT_PARAM_NAME,
108 paramMapping.paramName,
109 true,
110 g_extraParseResult);
111 AppExecFwk::BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
112 jsonObjectEnd,
113 INSIGHT_INTENT_PARAM_MAPPING_NAME,
114 paramMapping.paramMappingName,
115 false,
116 g_extraParseResult);
117 AppExecFwk::BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
118 jsonObjectEnd,
119 INSIGHT_INTENT_PARAM_CATEGORY,
120 paramMapping.paramCategory,
121 false,
122 g_extraParseResult);
123 }
124
from_json(const nlohmann::json & jsonObject,InsightIntentEntityInfo & entityInfo)125 void from_json(const nlohmann::json &jsonObject, InsightIntentEntityInfo &entityInfo)
126 {
127 TAG_LOGD(AAFwkTag::INTENT, "InsightIntentEntityInfo from json");
128 const auto &jsonObjectEnd = jsonObject.end();
129 AppExecFwk::BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
130 jsonObjectEnd,
131 INSIGHT_INTENT_ENTITY_DECORETOR_FILE,
132 entityInfo.decoratorFile,
133 true,
134 g_extraParseResult);
135 AppExecFwk::BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
136 jsonObjectEnd,
137 INSIGHT_INTENT_ENTITY_CLASS_NAME,
138 entityInfo.className,
139 true,
140 g_extraParseResult);
141 AppExecFwk::BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
142 jsonObjectEnd,
143 INSIGHT_INTENT_ENTITY_DECORETOR_TYPE,
144 entityInfo.decoratorType,
145 true,
146 g_extraParseResult);
147 AppExecFwk::BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
148 jsonObjectEnd,
149 INSIGHT_INTENT_ENTITY_ID,
150 entityInfo.entityId,
151 true,
152 g_extraParseResult);
153 AppExecFwk::BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
154 jsonObjectEnd,
155 INSIGHT_INTENT_ENTITY_CATEGORY,
156 entityInfo.entityCategory,
157 true,
158 g_extraParseResult);
159 AppExecFwk::BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
160 jsonObjectEnd,
161 INSIGHT_INTENT_ENTITY_PARENT_CLASS_NAME,
162 entityInfo.parentClassName,
163 false,
164 g_extraParseResult);
165
166 if (jsonObject.find(INSIGHT_INTENT_ENTITY_PARAMETERS) != jsonObjectEnd) {
167 if (jsonObject.at(INSIGHT_INTENT_ENTITY_PARAMETERS).is_object()) {
168 entityInfo.parameters = jsonObject[INSIGHT_INTENT_ENTITY_PARAMETERS].dump();
169 } else {
170 TAG_LOGE(AAFwkTag::INTENT, "type error: entity parameters not object");
171 g_extraParseResult = ERR_INVALID_VALUE;
172 }
173 }
174 }
175
from_json(const nlohmann::json & jsonObject,ExtractInsightIntentProfileInfo & insightIntentInfo)176 void from_json(const nlohmann::json &jsonObject, ExtractInsightIntentProfileInfo &insightIntentInfo)
177 {
178 TAG_LOGD(AAFwkTag::INTENT, "ExtractInsightIntentProfileInfo from json");
179 const auto &jsonObjectEnd = jsonObject.end();
180 AppExecFwk::BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
181 jsonObjectEnd,
182 INSIGHT_INTENT_DECORETOR_FILE,
183 insightIntentInfo.decoratorFile,
184 true,
185 g_extraParseResult);
186 AppExecFwk::BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
187 jsonObjectEnd,
188 INSIGHT_INTENT_DECORETOR_CLASS,
189 insightIntentInfo.decoratorClass,
190 true,
191 g_extraParseResult);
192 AppExecFwk::BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
193 jsonObjectEnd,
194 INSIGHT_INTENT_DECORETOR_TYPE,
195 insightIntentInfo.decoratorType,
196 true,
197 g_extraParseResult);
198 AppExecFwk::BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
199 jsonObjectEnd,
200 INSIGHT_INTENT_BUNDLE_NAME,
201 insightIntentInfo.bundleName,
202 true,
203 g_extraParseResult);
204 AppExecFwk::BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
205 jsonObjectEnd,
206 INSIGHT_INTENT_MODULE_NAME,
207 insightIntentInfo.moduleName,
208 true,
209 g_extraParseResult);
210 AppExecFwk::BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
211 jsonObjectEnd,
212 INSIGHT_INTENT_NAME,
213 insightIntentInfo.intentName,
214 true,
215 g_extraParseResult);
216 AppExecFwk::BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
217 jsonObjectEnd,
218 INSIGHT_INTENT_DOMAIN,
219 insightIntentInfo.domain,
220 true,
221 g_extraParseResult);
222 AppExecFwk::BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
223 jsonObjectEnd,
224 INSIGHT_INTENT_VERSION,
225 insightIntentInfo.intentVersion,
226 true,
227 g_extraParseResult);
228 AppExecFwk::BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
229 jsonObjectEnd,
230 INSIGHT_INTENT_DISPLAY_NAME,
231 insightIntentInfo.displayName,
232 true,
233 g_extraParseResult);
234 AppExecFwk::BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
235 jsonObjectEnd,
236 INSIGHT_INTENT_DISPLAY_DESCRIPTION,
237 insightIntentInfo.displayDescription,
238 false,
239 g_extraParseResult);
240 AppExecFwk::BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
241 jsonObjectEnd,
242 INSIGHT_INTENT_SCHEMA,
243 insightIntentInfo.schema,
244 false,
245 g_extraParseResult);
246 AppExecFwk::BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
247 jsonObjectEnd,
248 INSIGHT_INTENT_ICON,
249 insightIntentInfo.icon,
250 false,
251 g_extraParseResult);
252 AppExecFwk::BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
253 jsonObjectEnd,
254 INSIGHT_INTENT_LLM_DESCRIPTION,
255 insightIntentInfo.llmDescription,
256 false,
257 g_extraParseResult);
258 AppExecFwk::GetValueIfFindKey<std::vector<std::string>>(jsonObject,
259 jsonObjectEnd,
260 INSIGHT_INTENT_KEYWORDS,
261 insightIntentInfo.keywords,
262 JsonType::ARRAY,
263 false,
264 g_extraParseResult,
265 ArrayType::STRING);
266 AppExecFwk::BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
267 jsonObjectEnd,
268 INSIGHT_INTENT_EXAMPLE,
269 insightIntentInfo.example,
270 false,
271 g_extraParseResult);
272 AppExecFwk::BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
273 jsonObjectEnd,
274 INSIGHT_INTENT_URI,
275 insightIntentInfo.uri,
276 false,
277 g_extraParseResult);
278 AppExecFwk::GetValueIfFindKey<std::vector<LinkIntentParamProfileMapping>>(jsonObject,
279 jsonObjectEnd,
280 INSIGHT_INTENT_PARAM_MAPPING,
281 insightIntentInfo.paramMapping,
282 JsonType::ARRAY,
283 false,
284 g_extraParseResult,
285 ArrayType::OBJECT);
286 AppExecFwk::BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
287 jsonObjectEnd,
288 INSIGHT_INTENT_UI_ABILITY,
289 insightIntentInfo.uiAbility,
290 false,
291 g_extraParseResult);
292 AppExecFwk::BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
293 jsonObjectEnd,
294 INSIGHT_INTENT_PAGE_ROUTE_NAME,
295 insightIntentInfo.pagePath,
296 false,
297 g_extraParseResult);
298 AppExecFwk::BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
299 jsonObjectEnd,
300 INSIGHT_INTENT_NAVIGATION_ID,
301 insightIntentInfo.navigationId,
302 false,
303 g_extraParseResult);
304 AppExecFwk::BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
305 jsonObjectEnd,
306 INSIGHT_INTENT_NAV_DESTINATION_NAME,
307 insightIntentInfo.navDestinationName,
308 false,
309 g_extraParseResult);
310 AppExecFwk::BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
311 jsonObjectEnd,
312 INSIGHT_INTENT_ABILITY_NAME,
313 insightIntentInfo.abilityName,
314 false,
315 g_extraParseResult);
316 AppExecFwk::GetValueIfFindKey<std::vector<std::string>>(jsonObject,
317 jsonObjectEnd,
318 INSIGHT_INTENT_EXECUTE_MODE,
319 insightIntentInfo.executeMode,
320 JsonType::ARRAY,
321 false,
322 g_extraParseResult,
323 ArrayType::STRING);
324 AppExecFwk::BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
325 jsonObjectEnd,
326 INSIGHT_INTENT_FUNCTION_NAME,
327 insightIntentInfo.functionName,
328 false,
329 g_extraParseResult);
330 AppExecFwk::GetValueIfFindKey<std::vector<std::string>>(jsonObject,
331 jsonObjectEnd,
332 INSIGHT_INTENT_FUNCTION_PARAMS,
333 insightIntentInfo.functionParams,
334 JsonType::ARRAY,
335 false,
336 g_extraParseResult,
337 ArrayType::STRING);
338 AppExecFwk::GetValueIfFindKey<std::vector<InsightIntentEntityInfo>>(jsonObject,
339 jsonObjectEnd,
340 INSIGHT_INTENT_ENTITES,
341 insightIntentInfo.entities,
342 JsonType::ARRAY,
343 false,
344 g_extraParseResult,
345 ArrayType::OBJECT);
346 AppExecFwk::BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
347 jsonObjectEnd,
348 INSIGHT_INTENT_FORM_NAME,
349 insightIntentInfo.formName,
350 false,
351 g_extraParseResult);
352
353 if (jsonObject.find(INSIGHT_INTENT_PARAMETERS) != jsonObjectEnd) {
354 if (jsonObject.at(INSIGHT_INTENT_PARAMETERS).is_object()) {
355 insightIntentInfo.parameters = jsonObject[INSIGHT_INTENT_PARAMETERS].dump();
356 } else {
357 TAG_LOGE(AAFwkTag::INTENT, "type error: parameters not object");
358 g_extraParseResult = ERR_INVALID_VALUE;
359 }
360 }
361
362 if (jsonObject.find(INSIGHT_INTENT_RESULT) != jsonObjectEnd) {
363 if (jsonObject.at(INSIGHT_INTENT_RESULT).is_object()) {
364 insightIntentInfo.result = jsonObject[INSIGHT_INTENT_RESULT].dump();
365 } else {
366 TAG_LOGE(AAFwkTag::INTENT, "type error: result not object");
367 g_extraParseResult = ERR_INVALID_VALUE;
368 }
369 }
370 }
371
from_json(const nlohmann::json & jsonObject,ExtractInsightIntentProfileInfoVec & infos)372 void from_json(const nlohmann::json &jsonObject, ExtractInsightIntentProfileInfoVec &infos)
373 {
374 const auto &jsonObjectEnd = jsonObject.end();
375 AppExecFwk::GetValueIfFindKey<std::vector<ExtractInsightIntentProfileInfo>>(jsonObject,
376 jsonObjectEnd,
377 INSIGHT_INTENTS,
378 infos.insightIntents,
379 JsonType::ARRAY,
380 false,
381 g_extraParseResult,
382 ArrayType::OBJECT);
383 }
384
to_json(nlohmann::json & jsonObject,const LinkIntentParamProfileMapping & info)385 void to_json(nlohmann::json& jsonObject, const LinkIntentParamProfileMapping &info)
386 {
387 TAG_LOGI(AAFwkTag::INTENT, "LinkIntentParamProfileMapping to json");
388 jsonObject = nlohmann::json {
389 {INSIGHT_INTENT_PARAM_NAME, info.paramName},
390 {INSIGHT_INTENT_PARAM_MAPPING_NAME, info.paramMappingName},
391 {INSIGHT_INTENT_PARAM_CATEGORY, info.paramCategory}
392 };
393 }
394
to_json(nlohmann::json & jsonObject,const InsightIntentEntityInfo & info)395 void to_json(nlohmann::json& jsonObject, const InsightIntentEntityInfo &info)
396 {
397 TAG_LOGI(AAFwkTag::INTENT, "InsightIntentEntityInfo to json");
398 jsonObject = nlohmann::json {
399 {INSIGHT_INTENT_ENTITY_DECORETOR_FILE, info.decoratorFile},
400 {INSIGHT_INTENT_ENTITY_CLASS_NAME, info.className},
401 {INSIGHT_INTENT_ENTITY_DECORETOR_TYPE, info.decoratorType},
402 {INSIGHT_INTENT_ENTITY_ID, info.entityId},
403 {INSIGHT_INTENT_ENTITY_CATEGORY, info.entityCategory},
404 {INSIGHT_INTENT_ENTITY_PARENT_CLASS_NAME, info.parentClassName}
405 };
406
407 if (!info.parameters.empty()) {
408 auto parameters = nlohmann::json::parse(info.parameters, nullptr, false);
409 if (parameters.is_discarded()) {
410 TAG_LOGE(AAFwkTag::INTENT, "discarded entity parameters");
411 return;
412 }
413
414 jsonObject[INSIGHT_INTENT_ENTITY_PARAMETERS] = parameters;
415 }
416 }
417
to_json(nlohmann::json & jsonObject,const ExtractInsightIntentProfileInfo & info)418 void to_json(nlohmann::json& jsonObject, const ExtractInsightIntentProfileInfo& info)
419 {
420 TAG_LOGI(AAFwkTag::INTENT, "ExtractInsightIntentProfileInfo to json");
421
422 jsonObject = nlohmann::json {
423 {INSIGHT_INTENT_DECORETOR_FILE, info.decoratorFile},
424 {INSIGHT_INTENT_DECORETOR_CLASS, info.decoratorClass},
425 {INSIGHT_INTENT_DECORETOR_TYPE, info.decoratorType},
426 {INSIGHT_INTENT_BUNDLE_NAME, info.bundleName},
427 {INSIGHT_INTENT_MODULE_NAME, info.moduleName},
428 {INSIGHT_INTENT_NAME, info.intentName},
429 {INSIGHT_INTENT_DOMAIN, info.domain},
430 {INSIGHT_INTENT_VERSION, info.intentVersion},
431 {INSIGHT_INTENT_DISPLAY_NAME, info.displayName},
432 {INSIGHT_INTENT_DISPLAY_DESCRIPTION, info.displayDescription},
433 {INSIGHT_INTENT_SCHEMA, info.schema},
434 {INSIGHT_INTENT_ICON, info.icon},
435 {INSIGHT_INTENT_LLM_DESCRIPTION, info.llmDescription},
436 {INSIGHT_INTENT_KEYWORDS, info.keywords},
437 {INSIGHT_INTENT_EXAMPLE, info.example},
438 {INSIGHT_INTENT_URI, info.uri},
439 {INSIGHT_INTENT_PARAM_MAPPING, info.paramMapping},
440 {INSIGHT_INTENT_UI_ABILITY, info.uiAbility},
441 {INSIGHT_INTENT_PAGE_ROUTE_NAME, info.pagePath},
442 {INSIGHT_INTENT_NAVIGATION_ID, info.navigationId},
443 {INSIGHT_INTENT_NAV_DESTINATION_NAME, info.navDestinationName},
444 {INSIGHT_INTENT_ABILITY_NAME, info.abilityName},
445 {INSIGHT_INTENT_EXECUTE_MODE, info.executeMode},
446 {INSIGHT_INTENT_FUNCTION_NAME, info.functionName},
447 {INSIGHT_INTENT_FUNCTION_PARAMS, info.functionParams},
448 {INSIGHT_INTENT_FORM_NAME, info.formName},
449 {INSIGHT_INTENT_ENTITES, info.entities}
450 };
451
452 if (!info.parameters.empty()) {
453 auto parameters = nlohmann::json::parse(info.parameters, nullptr, false);
454 if (parameters.is_discarded()) {
455 TAG_LOGE(AAFwkTag::INTENT, "discarded parameters");
456 return;
457 }
458
459 jsonObject[INSIGHT_INTENT_PARAMETERS] = parameters;
460 }
461
462 if (!info.result.empty()) {
463 auto result = nlohmann::json::parse(info.result, nullptr, false);
464 if (result.is_discarded()) {
465 TAG_LOGE(AAFwkTag::INTENT, "discarded result");
466 return;
467 }
468
469 jsonObject[INSIGHT_INTENT_RESULT] = result;
470 }
471 }
472
CheckProfileSubIntentInfo(const ExtractInsightIntentProfileInfo & insightIntent)473 bool CheckProfileSubIntentInfo(const ExtractInsightIntentProfileInfo &insightIntent)
474 {
475 switch (StringToEnum(insightIntent.decoratorType)) {
476 case DecoratorType::DECORATOR_LINK:
477 if (insightIntent.uri.empty()) {
478 TAG_LOGE(AAFwkTag::INTENT, "empty uri, intentName: %{public}s", insightIntent.intentName.c_str());
479 return false;
480 }
481 for (const auto ¶mMapping: insightIntent.paramMapping) {
482 if (paramMapping.paramName.empty()) {
483 TAG_LOGE(AAFwkTag::INTENT, "empty paramName, intentName: %{public}s",
484 insightIntent.intentName.c_str());
485 return false;
486 }
487 }
488 break;
489 case DecoratorType::DECORATOR_PAGE:
490 if (insightIntent.pagePath.empty()) {
491 TAG_LOGE(AAFwkTag::INTENT, "empty pagePath, intentName: %{public}s",
492 insightIntent.intentName.c_str());
493 return false;
494 }
495 break;
496 case DecoratorType::DECORATOR_ENTRY:
497 if (insightIntent.abilityName.empty()) {
498 TAG_LOGE(AAFwkTag::INTENT, "empty abilityName, intentName: %{public}s",
499 insightIntent.intentName.c_str());
500 return false;
501 }
502 break;
503 case DecoratorType::DECORATOR_FUNCTION:
504 if (insightIntent.functionName.empty()) {
505 TAG_LOGE(AAFwkTag::INTENT, "empty functionName, intentName: %{public}s",
506 insightIntent.intentName.c_str());
507 return false;
508 }
509 break;
510 case DecoratorType::DECORATOR_FORM:
511 if (insightIntent.formName.empty() || insightIntent.abilityName.empty()) {
512 TAG_LOGE(AAFwkTag::INTENT, "empty formName or abilityName, intentName: %{public}s, "
513 "abilityName: %{public}s", insightIntent.intentName.c_str(), insightIntent.abilityName.c_str());
514 return false;
515 }
516 break;
517 default:
518 TAG_LOGE(AAFwkTag::INTENT, "invalid decoratorType: %{public}s", insightIntent.decoratorType.c_str());
519 return false;
520 }
521
522 return true;
523 }
524
CheckProfileInfo(const ExtractInsightIntentProfileInfo & insightIntent)525 bool CheckProfileInfo(const ExtractInsightIntentProfileInfo &insightIntent)
526 {
527 if (insightIntent.decoratorFile.empty() || insightIntent.decoratorClass.empty() ||
528 insightIntent.decoratorType.empty() || insightIntent.bundleName.empty() ||
529 insightIntent.moduleName.empty() || insightIntent.intentName.empty() || insightIntent.domain.empty() ||
530 insightIntent.intentVersion.empty() || insightIntent.displayName.empty()) {
531 TAG_LOGE(AAFwkTag::INTENT, "exist empty param, decoratorFile: %{public}s, decoratorClass: %{public}s, "
532 "decoratorType: %{public}s, bundleName: %{public}s, moduleName: %{public}s, intentName: %{public}s, "
533 "domain: %{public}s, intentVersion: %{public}s, displayName: %{public}s",
534 insightIntent.decoratorFile.c_str(), insightIntent.decoratorClass.c_str(),
535 insightIntent.decoratorType.c_str(), insightIntent.bundleName.c_str(), insightIntent.moduleName.c_str(),
536 insightIntent.intentName.c_str(), insightIntent.domain.c_str(), insightIntent.intentVersion.c_str(),
537 insightIntent.displayName.c_str());
538 return false;
539 }
540
541 for (const auto &entity: insightIntent.entities) {
542 if (entity.className.empty() || entity.entityId.empty()) {
543 TAG_LOGE(AAFwkTag::INTENT, "entity exist empty param, intentName: %{public}s, "
544 "className: %{public}s, entityId: %{public}s",
545 insightIntent.intentName.c_str(), entity.className.c_str(), entity.entityId.c_str());
546 return false;
547 }
548 }
549
550 return CheckProfileSubIntentInfo(insightIntent);
551 }
552
TransformToLinkInfo(const ExtractInsightIntentProfileInfo & insightIntent,InsightIntentLinkInfo & info)553 bool TransformToLinkInfo(const ExtractInsightIntentProfileInfo &insightIntent, InsightIntentLinkInfo &info)
554 {
555 info.uri = insightIntent.uri;
556 TAG_LOGD(AAFwkTag::INTENT, "uri: %{public}s", info.uri.c_str());
557 for (std::vector<LinkIntentParamProfileMapping>::const_iterator iter = insightIntent.paramMapping.begin();
558 iter != insightIntent.paramMapping.end(); iter++) {
559 LinkIntentParamMapping paramMapping;
560 paramMapping.paramName = (*iter).paramName;
561 TAG_LOGD(AAFwkTag::INTENT, "paramName: %{public}s", paramMapping.paramName.c_str());
562 paramMapping.paramMappingName = (*iter).paramMappingName;
563 TAG_LOGD(AAFwkTag::INTENT, "paramMappingName: %{public}s", paramMapping.paramMappingName.c_str());
564 paramMapping.paramCategory = (*iter).paramCategory;
565 TAG_LOGD(AAFwkTag::INTENT, "paramCategory: %{public}s", paramMapping.paramCategory.c_str());
566
567 info.paramMapping.push_back(paramMapping);
568 }
569 info.parameters = insightIntent.parameters;
570 TAG_LOGD(AAFwkTag::INTENT, "link parameters: %{public}s", info.parameters.c_str());
571 return true;
572 }
573
TransformToPageInfo(const ExtractInsightIntentProfileInfo & insightIntent,InsightIntentPageInfo & info)574 bool TransformToPageInfo(const ExtractInsightIntentProfileInfo &insightIntent, InsightIntentPageInfo &info)
575 {
576 info.uiAbility = insightIntent.uiAbility;
577 TAG_LOGD(AAFwkTag::INTENT, "uiAbility: %{public}s", info.uiAbility.c_str());
578 info.pagePath = insightIntent.pagePath;
579 TAG_LOGD(AAFwkTag::INTENT, "pagePath: %{public}s", info.pagePath.c_str());
580 info.navigationId = insightIntent.navigationId;
581 TAG_LOGD(AAFwkTag::INTENT, "navigationId: %{public}s", info.navigationId.c_str());
582 info.navDestinationName = insightIntent.navDestinationName;
583 TAG_LOGD(AAFwkTag::INTENT, "navDestinationName: %{public}s", info.navDestinationName.c_str());
584 info.parameters = insightIntent.parameters;
585 TAG_LOGD(AAFwkTag::INTENT, "page parameters: %{public}s", info.parameters.c_str());
586 return true;
587 }
588
TransformToEntryInfo(const ExtractInsightIntentProfileInfo & insightIntent,InsightIntentEntryInfo & info)589 bool TransformToEntryInfo(const ExtractInsightIntentProfileInfo &insightIntent, InsightIntentEntryInfo &info)
590 {
591 info.abilityName = insightIntent.abilityName;
592 for (const auto &executeMode: insightIntent.executeMode) {
593 auto mode = std::find_if(std::begin(executeModeMap), std::end(executeModeMap),
594 [&executeMode](const auto &item) {
595 return item.first == executeMode;
596 });
597 if (mode == executeModeMap.end()) {
598 TAG_LOGW(AAFwkTag::INTENT, "not support execute mode: %{public}s", executeMode.c_str());
599 continue;
600 }
601 info.executeMode.emplace_back(mode->second);
602 TAG_LOGI(AAFwkTag::INTENT, "mode: %{public}s", mode->first.c_str());
603 }
604 info.parameters = insightIntent.parameters;
605 TAG_LOGD(AAFwkTag::INTENT, "entry parameters: %{public}s", info.parameters.c_str());
606 return true;
607 }
608
TransformToFunctionInfo(const ExtractInsightIntentProfileInfo & insightIntent,InsightIntentFunctionInfo & info)609 bool TransformToFunctionInfo(const ExtractInsightIntentProfileInfo &insightIntent, InsightIntentFunctionInfo &info)
610 {
611 info.functionName = insightIntent.functionName;
612 TAG_LOGD(AAFwkTag::INTENT, "functionName: %{public}s", info.functionName.c_str());
613 info.functionParams.assign(insightIntent.functionParams.begin(), insightIntent.functionParams.end());
614 for (size_t i = 0; i < info.functionParams.size(); i++) {
615 TAG_LOGD(AAFwkTag::INTENT, "functionParams[%{public}zu]: %{public}s", i, info.functionParams[i].c_str());
616 }
617 info.parameters = insightIntent.parameters;
618 TAG_LOGD(AAFwkTag::INTENT, "function parameters: %{public}s", info.parameters.c_str());
619 return true;
620 }
621
TransformToFormInfo(const ExtractInsightIntentProfileInfo & insightIntent,InsightIntentFormInfo & info)622 bool TransformToFormInfo(const ExtractInsightIntentProfileInfo &insightIntent, InsightIntentFormInfo &info)
623 {
624 info.abilityName = insightIntent.abilityName;
625 info.formName = insightIntent.formName;
626 info.parameters = insightIntent.parameters;
627 TAG_LOGD(AAFwkTag::INTENT, "form parameters: %{public}s", info.parameters.c_str());
628 return true;
629 }
630
TransformTo(const std::string & profileStr,ExtractInsightIntentProfileInfoVec & intentInfos)631 bool ExtractInsightIntentProfile::TransformTo(const std::string &profileStr,
632 ExtractInsightIntentProfileInfoVec &intentInfos)
633 {
634 TAG_LOGD(AAFwkTag::INTENT, "transform profileStr: %{public}s", profileStr.c_str());
635 auto jsonObject = nlohmann::json::parse(profileStr, nullptr, false);
636 if (jsonObject.is_discarded()) {
637 TAG_LOGE(AAFwkTag::INTENT, "discarded jsonObject, profileStr: %{public}s", profileStr.c_str());
638 return false;
639 }
640
641 std::lock_guard<std::mutex> lock(g_extraMutex);
642 g_extraParseResult = ERR_OK;
643 intentInfos = jsonObject.get<ExtractInsightIntentProfileInfoVec>();
644 if (g_extraParseResult != ERR_OK) {
645 TAG_LOGE(AAFwkTag::INTENT, "parse result: %{public}d, profileStr: %{public}s",
646 g_extraParseResult, profileStr.c_str());
647 g_extraParseResult = ERR_OK;
648 return false;
649 }
650
651 for (const auto &insightIntent : intentInfos.insightIntents) {
652 if (!CheckProfileInfo(insightIntent)) {
653 return false;
654 }
655 }
656
657 TAG_LOGI(AAFwkTag::INTENT, "transform success, size: %{public}zu", intentInfos.insightIntents.size());
658 return true;
659 }
660
ToJson(const ExtractInsightIntentProfileInfo & info,nlohmann::json & jsonObject)661 bool ExtractInsightIntentProfile::ToJson(const ExtractInsightIntentProfileInfo &info, nlohmann::json &jsonObject)
662 {
663 TAG_LOGD(AAFwkTag::INTENT, "to json");
664 nlohmann::json subJsonObject = info;
665 if (subJsonObject.is_discarded()) {
666 TAG_LOGE(AAFwkTag::INTENT, "bad insight intent info");
667 return false;
668 }
669
670 jsonObject[INSIGHT_INTENTS] = nlohmann::json::array({ subJsonObject });
671 TAG_LOGD(AAFwkTag::INTENT, "to json string: %{public}s", jsonObject.dump().c_str());
672 return true;
673 }
674
ProfileInfoFormat(const ExtractInsightIntentProfileInfo & insightIntent,ExtractInsightIntentInfo & info)675 bool ExtractInsightIntentProfile::ProfileInfoFormat(const ExtractInsightIntentProfileInfo &insightIntent,
676 ExtractInsightIntentInfo &info)
677 {
678 bool ret = false;
679 if (!CheckProfileInfo(insightIntent)) {
680 return false;
681 }
682
683 info.decoratorFile = insightIntent.decoratorFile;
684 info.decoratorClass = insightIntent.decoratorClass;
685 info.displayDescription = insightIntent.displayDescription;
686 info.domain = insightIntent.domain;
687 info.intentVersion = insightIntent.intentVersion;
688 info.schema = insightIntent.schema;
689 info.icon = insightIntent.icon;
690 info.llmDescription = insightIntent.llmDescription;
691 info.example = insightIntent.example;
692 info.result = insightIntent.result;
693 info.keywords.assign(insightIntent.keywords.begin(), insightIntent.keywords.end());
694 info.entities = insightIntent.entities;
695 TAG_LOGD(AAFwkTag::INTENT, "entities size: %{public}zu", info.entities.size());
696 for (auto iter = info.entities.begin(); iter != info.entities.end(); iter++) {
697 TAG_LOGD(AAFwkTag::INTENT, "entity decoratorFile: %{public}s, className: %{public}s, "
698 "decoratorType: %{public}s, entityId: %{public}s, entityCategory: %{public}s, "
699 "parentClassName: %{public}s, parameters: %{public}s",
700 (*iter).decoratorFile.c_str(), (*iter).className.c_str(), (*iter).decoratorType.c_str(),
701 (*iter).entityId.c_str(), (*iter).entityCategory.c_str(), (*iter).parentClassName.c_str(),
702 (*iter).parameters.c_str());
703 }
704
705 info.genericInfo.bundleName = insightIntent.bundleName;
706 TAG_LOGD(AAFwkTag::INTENT, "bundleName: %{public}s", info.genericInfo.bundleName.c_str());
707 info.genericInfo.moduleName = insightIntent.moduleName;
708 TAG_LOGD(AAFwkTag::INTENT, "moduleName: %{public}s", info.genericInfo.moduleName.c_str());
709 info.genericInfo.intentName = insightIntent.intentName;
710 TAG_LOGD(AAFwkTag::INTENT, "intentName: %{public}s", info.genericInfo.intentName.c_str());
711 info.genericInfo.displayName = insightIntent.displayName;
712 TAG_LOGD(AAFwkTag::INTENT, "displayName: %{public}s", info.genericInfo.displayName.c_str());
713 info.genericInfo.decoratorType = insightIntent.decoratorType;
714 TAG_LOGD(AAFwkTag::INTENT, "decoratorType: %{public}s", info.genericInfo.decoratorType.c_str());
715
716 switch (StringToEnum(insightIntent.decoratorType)) {
717 case DecoratorType::DECORATOR_LINK:
718 ret = TransformToLinkInfo(insightIntent, info.genericInfo.get<InsightIntentLinkInfo>());
719 break;
720 case DecoratorType::DECORATOR_PAGE:
721 ret = TransformToPageInfo(insightIntent, info.genericInfo.get<InsightIntentPageInfo>());
722 break;
723 case DecoratorType::DECORATOR_ENTRY:
724 ret = TransformToEntryInfo(insightIntent, info.genericInfo.get<InsightIntentEntryInfo>());
725 break;
726 case DecoratorType::DECORATOR_FUNCTION:
727 ret = TransformToFunctionInfo(insightIntent, info.genericInfo.get<InsightIntentFunctionInfo>());
728 break;
729 case DecoratorType::DECORATOR_FORM:
730 ret = TransformToFormInfo(insightIntent, info.genericInfo.get<InsightIntentFormInfo>());
731 break;
732 default:
733 TAG_LOGE(AAFwkTag::INTENT, "invalid decoratorType: %{public}s", insightIntent.decoratorType.c_str());
734 return false;
735 }
736
737 return ret;
738 }
739 } // namespace AbilityRuntime
740 } // namespace OHOS
741