Overview

In this article, we cover the skillRunState methods available as part of the skillContext when writing your skills, which can help reduce the need to repeat logic in your skill code.

 

In this Article

 

What is the skillRunState?

The skillRunState allows for storing and retrieving data at any point in skill execution and is only active for one skill run cycle. The data can be any value type and is independent of eventData.

 

When to use the skillRunState?

The skill run state can be used when a skill writer may need to repeat logic in the shouldIRun and Run functions. Some example uses include:

  • If data needed to validate when a skill should run, is also needed when the skill actually runs, then the data can be stored in the shouldIRun and retrieved later in the run.
  • If data is needed throughout various points of the shouldIRun or run, the data can be stored once and retrieved several times.
  • If several types of data are need throughout various points of the shoudlIRun or run, multiple types of data can be stored and retrieved as need.

 

How to use the skillRunState

Storing Data

To store data as part of the skillRunState, you'll enter a key-value pair as part of the setSkillRunState method.

skillContext.setSkillRunState(keyName, keyValue)

As an example, we have a key, "data", that is storing an object, {fieldOne: "Hello"} as part of the shouldIRun.

export async function shouldIRun(skillContext:andiSkills.ISkillContext): Promise {
    if (!skillContext.powers.precisionLender.opportunity.isLoanAccountChangeEvent())
        return skillContext.powers.andi.shouldIRun.shouldIRunFalse(); 

    await skillContext.setSkillRunState("data", { fieldOne: "Hello" });

    return skillContext.powers.andi.shouldIRun.shouldIRunTrue(); 
} 

Keep in mind that if storing multiple sets of data, each setSkillRunState will need its own unique keyName. Otherwise, if the same keyName is used, the most recent key will override the previous data.

 

Retrieving Data

To retrieve your stored data as part of the skillRunState, use the getSkillRunState method with the corresponding key, which will then allow you to retrieve the value.

let keyValue = await skillContext.getSkillRunState(keyName)

Continuing with our previous example, we're going to retrieve the data stored in our shouldIRun file in the run file to use as part of the field tag message.

export async function run(skillContext: andiSkills.ISkillContext) : Promise {

    var data = await skillContext.getSkillRunState("data"); 
//the field tag message should say: The fieldOne data value is Hello var message = "The fieldOne data value is " + data.fieldOne; return skillContext.powers.andi.fieldTag.sendFieldTag(andiSkills.FieldTagTypes.Info, "run-state-test-data", -1, message, [], null, function(api, model, hash, custom) { return true; }, null, null, null, null); } 
Was this article helpful?
1 out of 1 found this helpful