13

public async getMasterIntent(userText: string, id?: string, idType?: string): Promise<any> {
    const masterIntentResponse: MasterIntentClassifierResponse = new MasterIntentClassifierResponse();

    // Start classifyIntent, skip words retrieval, and handleCacheLogic in parallel
    const mlResponsePromise = this.classifyIntent(userText);
    const skipWordsPromise = this.configService.get<string[]>(`skipWords`) || [];
    const cacheLogicPromise = id && idType ? this.handleCacheLogic(masterIntentResponse, id, idType) : Promise.resolve();

    // Await the classifyIntent and skipWords results
    const [mlResponse, skipWords] = await Promise.all([mlResponsePromise, skipWordsPromise]);

    // Process the highLevelIntent and check if it should be skipped
    const highLevelIntent = mlResponse?.intents.length ? mlResponse.intents[0].toLowerCase().trim() : 'unknown';
    const isSkip = skipWords.some(word => highLevelIntent.toLowerCase().includes(word.toLowerCase()));

    let intentResp: string[] | undefined;

    // If not skipping, initiate getIntentAIData call in parallel with other operations
    if (highLevelIntent !== 'unknown' && !isSkip) {
        const intentAIDataPromise = this.getIntentAIData(userText, highLevelIntent);

        // Await the intent AI data
        const response = await intentAIDataPromise;
        intentResp = response?.chat_response.split(',');
        masterIntentResponse.subIntent = intentResp.map((i: string) => i.toLowerCase().trim());
    } else {
        masterIntentResponse.subIntent = [highLevelIntent];
    }

    masterIntentResponse.userText = userText;
    masterIntentResponse.statusFlags = {
        expeditedPrescriptionEligibleCount: -1,
        prescriptionEligibleCount: -1,
        cancelPrescriptionEligibleCount: -1,
        refillablePrescriptionEligibleCount: -1,
        transferPrescriptionEligibleCount: -1,
        prescriptionStatus: [],
    };

    // Filter out 'help' from subIntents if necessary
    const valueToRemove = 'help';
    masterIntentResponse.subIntent =
        masterIntentResponse.subIntent.length > 1
            ? masterIntentResponse.subIntent.filter((item) => item !== valueToRemove)
            : masterIntentResponse.subIntent;

    // Wait for the cache logic if it was started earlier
    await cacheLogicPromise;

    // Now, handle intent matching
    let matchedIntent: MatchedIntentResponse = await this.matchIntent(masterIntentResponse.subIntent);

    if (masterIntentResponse.subIntent.includes('unknown')) {
        masterIntentResponse.intent = String(IntentClassEnum.INTENT_UNKNOWN);
    } else if (matchedIntent.matchedAttributes.mainIntent === 'multiintent') {
        matchedIntent = await this.matchExceptionIntent(masterIntentResponse.subIntent);
    }

    masterIntentResponse.intent = matchedIntent.matchedAttributes.mainIntent;
    masterIntentResponse.fillerText = matchedIntent.partialFiller;
    masterIntentResponse.promptId = matchedIntent.matchedAttributes.promptID?.trim() || '-1';
    masterIntentResponse.templateId = matchedIntent.matchedAttributes.dataTemplate?.trim() || '-1';
    masterIntentResponse.ruleNo = matchedIntent.matchedAttributes.ruleNo?.trim() || '-1';
    masterIntentResponse.seqNo = matchedIntent.matchedAttributes.seqNo;

    this.logger.addFunctionalTags('masterIntentResponse.intent', `${masterIntentResponse.intent}`);
    this.logger.addFunctionalTags('masterIntentResponse.subIntent', `${masterIntentResponse.subIntent}`);
    this.logger.addFunctionalTags('masterIntentResponse.fillerText', `${masterIntentResponse.fillerText}`);
    this.logger.addFunctionalTags('masterIntentResponse.promptId', `${masterIntentResponse.promptId}`);
    this.logger.addFunctionalTags('masterIntentResponse.templateId', `${masterIntentResponse.templateId}`);
    this.logger.addFunctionalTags('masterIntentResponse.ruleId', `${matchedIntent.matchedAttributes.ruleNo}`);
    this.logger.addFunctionalTags('masterIntentResponse.seqNo', `${matchedIntent.matchedAttributes.seqNo}`);

    return masterIntentResponse;
}