Name

global.AutoResolutionIntentPredictionStage

Description

No description available

Script

var AutoResolutionIntentPredictionStage = Class.create();
AutoResolutionIntentPredictionStage.prototype = Object.extendsObject(AutoResolutionTaskProcessingStage, {

  getStateValue: function() {
  	return AutoResolutionConstants.TASK_PROCESSING_STATE.INTENT_PREDICTION;
  },

  execute: function(contextFieldValueMap) {
  	this.prepare(contextFieldValueMap, this.type);

  	if (!this.shouldAgentZeroPredictionRun()) {
  		this._setIntentTopicState(AutoResolutionConstants.INTENT_TOPIC_STATE['NO_INTENT']);
  		this.setContextReason(AutoResolutionConstants.NO_INTENT_REASON['INTENT_PREDICTION_NOT_ENABLED']);
  		return this.response;
  	}

  	if (!this.canAgentZeroPredictionRun()) {
  		this._setIntentTopicState(AutoResolutionConstants.INTENT_TOPIC_STATE['NO_INTENT']);
  		this.setContextReason(AutoResolutionConstants.NO_INTENT_REASON['INACTIVE_LANGUAGE']);
  		return this.response;
  	}

  	// get the solution name and version that will be used for AZ prediction
  	var solutionInfo = AutoResolutionLanguageHelper.getSolutionNameAndVersionForLanguage(this.configId, this.languageCode);

  	// empty or null check is not required on results because we have done all the validations and an active language config record should exist for the give config and language code
  	this.setContextValue("ml_solution_name", solutionInfo.solution_name);
  	this.setContextValue("ml_solution_version", solutionInfo.solution_version);

  	try {
  		var mlHelper = new AutoResolutionMLHelper(this.configId);
  		var startTime = new Date().getTime();
  		var intentList = mlHelper.predict(this.taskGr, this.languageCode);
  		var endTime = new Date().getTime();
  		this.LOGGER.info('Time taken for Auto-Resolution prediction for task {0} is {1}ms',
  			this.taskGr.getValue('number'), (endTime - startTime));

  		intentList = intentList || {};
  		var intentAndTopicDetails = new AutoResolutionPredictionHelper(this.configId).getIntentAndTopicDetails(intentList[this.taskId]);
  		this.persistIntents(intentAndTopicDetails.intents);

  		this.setContextValue('nlu_intent', intentAndTopicDetails.nluIntent);
  		this.setContextValue('matched_topic', intentAndTopicDetails.matchedTopic);
  		this._setIntentTopicState(intentAndTopicDetails.intentTopicState);

  		var contextReason = intentList.size === 0 
  			? AutoResolutionConstants.NO_INTENT_REASON['API_ERROR'] 
  			: intentAndTopicDetails.intentTopicStateReason;
  		this.setContextReason(contextReason);

  	} catch (error) {
  		this.LOGGER.error('Error calling Agent Zero predictions for task {0}. Error: {1}', this.taskGr.getValue('number'), error);
  		this.setError(error);
  	}
  	
  	return this.response;
  },

  shouldAgentZeroPredictionRun: function() {
  	return AutoResolutionUtil.checkSolutionWithCapabilityExists(this.configId,
  		'active=true^' + AutoResolutionConstants.AZ_ENCODED_QUERY);
  },

  /**
   * We check if given session language has a trained ML model and if that language config is active
   * @returns {boolean}
   */
  canAgentZeroPredictionRun: function() {
  	if (AutoResolutionTaskHelper.taskCreatedInSupportedSessionLanguage(this.configGr.getUniqueValue(), this.languageCode)) {
  		var trainedLanguageCodes = AutoResolutionLanguageHelper.getActiveLanguages(this.configGr.getUniqueValue());
  		if (!gs.nil(trainedLanguageCodes) && trainedLanguageCodes.length > 0)
  			return trainedLanguageCodes.indexOf(this.languageCode) >= 0;
  	}
  	return false;
  },

  /**
   * Persists all the intents in Matched intents table
   * @param intents
   */
  persistIntents: function(intents) {
  	var matchedIntentsGr = new GlideRecord('sys_cs_auto_resolution_matched_intents');

  	for (var i=0; i<intents.length; i++) {
  		var nluIntent = intents[i]['predictedValue'];
  		var confidence = intents[i]['confidence'];
  		
  		var intentTopicRetObj = new AutoResolutionPredictionHelper(this.configId)
  			.getMatchedTopicFromIntentMapTable(nluIntent);
  		var matchedTopicSysId = intentTopicRetObj.matchedTopicSysId;
  		matchedIntentsGr.setValue('nlu_intent', nluIntent);
  		matchedIntentsGr.setValue('confidence', confidence);
  		matchedIntentsGr.setValue('context', this.contextId);

  		if (!gs.nil(matchedTopicSysId))
  			matchedIntentsGr.setValue('matched_topic', matchedTopicSysId);
  		matchedIntentsGr.insert();

  		this.LOGGER.debug('Created matched intent for context={0} and values: nlu_intent={1}, confidence={2}, matched_topic={3}',
  			this.contextId, nluIntent,confidence, matchedTopicSysId);
  	}
  },

  _setIntentTopicState: function(state) {
  	this.setContextValue('intent_topic_state', state);
  },

  type: 'AutoResolutionIntentPredictionStage'
});

Sys ID

cee74ce253220110af71ddeeff7b1209

Offical Documentation

Official Docs: