Skip to content

Building a new Intent

Javi Jiménez edited this page Jun 26, 2016 · 1 revision

Remember that when we set an intent in a determinate Ava instance we only need code:

import intentName from './intentName.js';

ava.intent(intentName, action);

Ava will process your intent definition and will queue it on intents list to execute. But... what is your intent definition?, well you will receive two parameters:

  • state: the actual object state.
  • actions: a array of actions to execute if intent is successful.

Lets see the basic definition of your intent:

intentName.js

import { resolve } from 'ava-ia/lib/helpers'

export default (state, actions) => {
  resolve(state);
};

All intents must be resolved with the state (like a promise) but maybe your function it isn't a promise (async) for that reason we build the helper resolve. Just call it and your intent will be part of the factory of intents. Now we will see a complete example, our intent will:

  • check if a list of terms are part of state attributes tokens and classifier
  • check if the sentence has a specific syntax
'use strict';

import { factoryActions, intersect, syntax, resolve } from 'ava-ia/lib/helpers'
// -- Internal
const TERMS = [ 'film', 'movie' ];
const RULES = [
  '[Person] want see [Noun]',
];

export default (state, actions) => {
  const tokens = intersect(TERMS, state.tokens);
  const classifiers = intersect(TERMS, state.classifier);
  const match = syntax(state.sentence, RULES);

  if (tokens || classifiers || match) {
    return factoryActions(state, actions);
  } else {
    return resolve(state);
  }
};

As you can see, if we have tokens, or classifiers or match fulfilled we will call to our actions using the helper factoryActions. Easy right?

Feel free to offer new features, improvements or anything you can think of. This project makes sense with your participation and experience using Ava.

Clone this wiki locally