13

<p>public async getMasterIntent(userText: string, id?: string, idType?: string): Promise&lt;any&gt; {<br /> &nbsp; &nbsp; const masterIntentResponse: MasterIntentClassifierResponse = new MasterIntentClassifierResponse();</p> <p>&nbsp; &nbsp; // Start classifyIntent, skip words retrieval, and handleCacheLogic in parallel<br /> &nbsp; &nbsp; const mlResponsePromise = this.classifyIntent(userText);<br /> &nbsp; &nbsp; const skipWordsPromise = this.configService.get&lt;string[]&gt;(`skipWords`) || [];<br /> &nbsp; &nbsp; const cacheLogicPromise = id &amp;&amp; idType ? this.handleCacheLogic(masterIntentResponse, id, idType) : Promise.resolve();</p> <p>&nbsp; &nbsp; // Await the classifyIntent and skipWords results<br /> &nbsp; &nbsp; const [mlResponse, skipWords] = await Promise.all([mlResponsePromise, skipWordsPromise]);</p> <p>&nbsp; &nbsp; // Process the highLevelIntent and check if it should be skipped<br /> &nbsp; &nbsp; const highLevelIntent = mlResponse?.intents.length ? mlResponse.intents[0].toLowerCase().trim() : &#39;unknown&#39;;<br /> &nbsp; &nbsp; const isSkip = skipWords.some(word =&gt; highLevelIntent.toLowerCase().includes(word.toLowerCase()));</p> <p>&nbsp; &nbsp; let intentResp: string[] | undefined;</p> <p>&nbsp; &nbsp; // If not skipping, initiate getIntentAIData call in parallel with other operations<br /> &nbsp; &nbsp; if (highLevelIntent !== &#39;unknown&#39; &amp;&amp; !isSkip) {<br /> &nbsp; &nbsp; &nbsp; &nbsp; const intentAIDataPromise = this.getIntentAIData(userText, highLevelIntent);</p> <p>&nbsp; &nbsp; &nbsp; &nbsp; // Await the intent AI data<br /> &nbsp; &nbsp; &nbsp; &nbsp; const response = await intentAIDataPromise;<br /> &nbsp; &nbsp; &nbsp; &nbsp; intentResp = response?.chat_response.split(&#39;,&#39;);<br /> &nbsp; &nbsp; &nbsp; &nbsp; masterIntentResponse.subIntent = intentResp.map((i: string) =&gt; i.toLowerCase().trim());<br /> &nbsp; &nbsp; } else {<br /> &nbsp; &nbsp; &nbsp; &nbsp; masterIntentResponse.subIntent = [highLevelIntent];<br /> &nbsp; &nbsp; }</p> <p>&nbsp; &nbsp; masterIntentResponse.userText = userText;<br /> &nbsp; &nbsp; masterIntentResponse.statusFlags = {<br /> &nbsp; &nbsp; &nbsp; &nbsp; expeditedPrescriptionEligibleCount: -1,<br /> &nbsp; &nbsp; &nbsp; &nbsp; prescriptionEligibleCount: -1,<br /> &nbsp; &nbsp; &nbsp; &nbsp; cancelPrescriptionEligibleCount: -1,<br /> &nbsp; &nbsp; &nbsp; &nbsp; refillablePrescriptionEligibleCount: -1,<br /> &nbsp; &nbsp; &nbsp; &nbsp; transferPrescriptionEligibleCount: -1,<br /> &nbsp; &nbsp; &nbsp; &nbsp; prescriptionStatus: [],<br /> &nbsp; &nbsp; };</p> <p>&nbsp; &nbsp; // Filter out &#39;help&#39; from subIntents if necessary<br /> &nbsp; &nbsp; const valueToRemove = &#39;help&#39;;<br /> &nbsp; &nbsp; masterIntentResponse.subIntent =<br /> &nbsp; &nbsp; &nbsp; &nbsp; masterIntentResponse.subIntent.length &gt; 1<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ? masterIntentResponse.subIntent.filter((item) =&gt; item !== valueToRemove)<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : masterIntentResponse.subIntent;</p> <p>&nbsp; &nbsp; // Wait for the cache logic if it was started earlier<br /> &nbsp; &nbsp; await cacheLogicPromise;</p> <p>&nbsp; &nbsp; // Now, handle intent matching<br /> &nbsp; &nbsp; let matchedIntent: MatchedIntentResponse = await this.matchIntent(masterIntentResponse.subIntent);</p> <p>&nbsp; &nbsp; if (masterIntentResponse.subIntent.includes(&#39;unknown&#39;)) {<br /> &nbsp; &nbsp; &nbsp; &nbsp; masterIntentResponse.intent = String(IntentClassEnum.INTENT_UNKNOWN);<br /> &nbsp; &nbsp; } else if (matchedIntent.matchedAttributes.mainIntent === &#39;multiintent&#39;) {<br /> &nbsp; &nbsp; &nbsp; &nbsp; matchedIntent = await this.matchExceptionIntent(masterIntentResponse.subIntent);<br /> &nbsp; &nbsp; }</p> <p>&nbsp; &nbsp; masterIntentResponse.intent = matchedIntent.matchedAttributes.mainIntent;<br /> &nbsp; &nbsp; masterIntentResponse.fillerText = matchedIntent.partialFiller;<br /> &nbsp; &nbsp; masterIntentResponse.promptId = matchedIntent.matchedAttributes.promptID?.trim() || &#39;-1&#39;;<br /> &nbsp; &nbsp; masterIntentResponse.templateId = matchedIntent.matchedAttributes.dataTemplate?.trim() || &#39;-1&#39;;<br /> &nbsp; &nbsp; masterIntentResponse.ruleNo = matchedIntent.matchedAttributes.ruleNo?.trim() || &#39;-1&#39;;<br /> &nbsp; &nbsp; masterIntentResponse.seqNo = matchedIntent.matchedAttributes.seqNo;</p> <p>&nbsp; &nbsp; this.logger.addFunctionalTags(&#39;masterIntentResponse.intent&#39;, `${masterIntentResponse.intent}`);<br /> &nbsp; &nbsp; this.logger.addFunctionalTags(&#39;masterIntentResponse.subIntent&#39;, `${masterIntentResponse.subIntent}`);<br /> &nbsp; &nbsp; this.logger.addFunctionalTags(&#39;masterIntentResponse.fillerText&#39;, `${masterIntentResponse.fillerText}`);<br /> &nbsp; &nbsp; this.logger.addFunctionalTags(&#39;masterIntentResponse.promptId&#39;, `${masterIntentResponse.promptId}`);<br /> &nbsp; &nbsp; this.logger.addFunctionalTags(&#39;masterIntentResponse.templateId&#39;, `${masterIntentResponse.templateId}`);<br /> &nbsp; &nbsp; this.logger.addFunctionalTags(&#39;masterIntentResponse.ruleId&#39;, `${matchedIntent.matchedAttributes.ruleNo}`);<br /> &nbsp; &nbsp; this.logger.addFunctionalTags(&#39;masterIntentResponse.seqNo&#39;, `${matchedIntent.matchedAttributes.seqNo}`);</p> <p>&nbsp; &nbsp; return masterIntentResponse;<br /> }<br /> &nbsp;</p>