-
Notifications
You must be signed in to change notification settings - Fork 55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve multi-executors RuleTypes flow #26
Changes from 4 commits
fdb982f
43cce1b
42556b7
ef4cafd
084a843
b8ced27
85a9add
7e50dcb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,9 +2,11 @@ import { YveBotOptions, Rule, Answer } from '../types'; | |
import { Store, StoreData } from './store'; | ||
import { Controller } from './controller'; | ||
import { Actions } from './actions'; | ||
import { Types } from './types'; | ||
import { Types, WaitForUserInput } from './types'; | ||
import { Validators } from './validators'; | ||
|
||
import * as Exceptions from './exceptions'; | ||
|
||
function sanitizeRule(rule: Rule): Rule { | ||
if (typeof rule === 'string') { | ||
return { message: rule }; | ||
|
@@ -23,6 +25,9 @@ function sanitizeRule(rule: Rule): Rule { | |
} | ||
|
||
export class YveBot { | ||
static Exceptions: any; | ||
static WaitForUserInputExecutor: typeof WaitForUserInput; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. try without typeof |
||
|
||
private handlers: { [handler: string]: Array<() => any> }; | ||
private _rules?: Rule[]; | ||
|
||
|
@@ -121,3 +126,7 @@ export class YveBot { | |
YveBot.prototype.types = new Types; | ||
YveBot.prototype.actions = new Actions; | ||
YveBot.prototype.validators = new Validators; | ||
|
||
|
||
YveBot.Exceptions = Exceptions; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. use |
||
YveBot.WaitForUserInputExecutor = WaitForUserInput; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,14 @@ | ||
import { Rule, RuleNext, Answer } from '../types'; | ||
import { YveBot } from './bot'; | ||
import { ValidatorError, InvalidAttributeError, RuleNotFound } from './exceptions'; | ||
import { | ||
InvalidAttributeError, | ||
PauseRuleTypeExecutors, | ||
RuleNotFound, | ||
ValidatorError, | ||
} from './exceptions'; | ||
import * as utils from './utils'; | ||
|
||
|
||
async function validateAnswer( | ||
answers: Answer | Answer[], | ||
rule: Rule, | ||
|
@@ -190,12 +196,36 @@ export class Controller { | |
return this.bot.store.get(`executors.${rule.name}.currentIdx`) || 0; | ||
} | ||
|
||
setRuleExecutorIndex(rule: Rule, value?: any): void { | ||
if (value !== undefined) { | ||
this.bot.store.set(`executors.${rule.name}.currentIdx`, value); | ||
} else { | ||
this.bot.store.unset(`executors.${rule.name}.currentIdx`); | ||
incRuleExecutorIndex(rule: Rule): void { | ||
this.bot.store.set( | ||
`executors.${rule.name}.currentIdx`, this.getRuleExecutorIndex(rule) + 1 | ||
); | ||
} | ||
|
||
resetRuleExecutorIndex(rule: Rule): void { | ||
this.bot.store.unset(`executors.${rule.name}.currentIdx`); | ||
} | ||
|
||
async executeRuleTypeExecutors(rule: Rule, lastAnswer: Answer | Answer[]): Promise<any> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There is no guarantee that return will be answer... the return will be whatever transform + validators returns There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
const { bot } = this; | ||
const executorIdx = this.getRuleExecutorIndex(rule); | ||
const executors = bot.types[rule.type].executors; | ||
|
||
const executor = executors.slice(executorIdx)[0] || {}; | ||
const { transform = (...args) => Promise.resolve(args[0]) } = executor; | ||
const answer = await ( | ||
transform(lastAnswer, rule, bot) | ||
.then(answer => validateAnswer(answer, rule, bot, this.getRuleExecutorIndex(rule))) | ||
); | ||
|
||
const completed = (this.getRuleExecutorIndex(rule) === executors.length-1); | ||
if (!completed) { | ||
this.incRuleExecutorIndex(rule); | ||
return await this.executeRuleTypeExecutors(rule, answer); | ||
} | ||
|
||
this.resetRuleExecutorIndex(rule); | ||
return answer; | ||
} | ||
|
||
async receiveMessage(message: Answer | Answer[]): Promise<this> { | ||
|
@@ -208,24 +238,19 @@ export class Controller { | |
} | ||
|
||
let answer; | ||
const { executors } = bot.types[rule.type]; | ||
const executor = executors[this.getRuleExecutorIndex(rule)] || {}; | ||
const { transform = (...args) => Promise.resolve(args[0]) } = executor; | ||
try { | ||
answer = await transform(message, rule, bot).then(answer => validateAnswer( | ||
answer, rule, bot, this.getRuleExecutorIndex(rule) | ||
)); | ||
|
||
if ( this.getRuleExecutorIndex(rule) < executors.length-1 ) { | ||
this.setRuleExecutorIndex(rule, this.getRuleExecutorIndex(rule)+1); | ||
bot.dispatch('hear'); | ||
return this; | ||
} | ||
|
||
this.setRuleExecutorIndex(rule); | ||
answer = await this.executeRuleTypeExecutors(rule, message); | ||
} catch (e) { | ||
let expectedError = false; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [minor] renames to |
||
if (e instanceof ValidatorError) { | ||
expectedError = true; | ||
await this.sendMessage(e.message, rule); | ||
} if (e instanceof PauseRuleTypeExecutors) { | ||
expectedError = true; | ||
this.incRuleExecutorIndex(rule); | ||
} | ||
|
||
if (expectedError) { | ||
bot.dispatch('hear'); | ||
return this; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,3 +20,8 @@ export function RuleNotFound(name: string, indexes: { [k: string]: number }) { | |
this.message = `Rule "${name}" not found in indexes`; | ||
this.indexes = indexes; | ||
} | ||
|
||
export function PauseRuleTypeExecutors(name: string) { | ||
this.key = 'pauseRuleTypeExecutors'; | ||
this.message = `Rule "${name}" should pause exection`; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. typo |
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,19 @@ | ||
import { uniq } from 'lodash'; | ||
import { DefineModule } from './module'; | ||
import { Rule, Answer } from '../types'; | ||
import { PauseRuleTypeExecutors } from './exceptions'; | ||
import { Rule, Answer, RuleType, RuleTypeExecutor } from '../types'; | ||
import * as utils from './utils'; | ||
|
||
const types = { | ||
|
||
export const WaitForUserInput: RuleTypeExecutor = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. move it to |
||
validators: [{ | ||
function: (_: Answer, rule: Rule): void => { | ||
throw new PauseRuleTypeExecutors(rule.name); | ||
} | ||
}] | ||
}; | ||
|
||
const types: { [name: string]: RuleType } = { | ||
Any: { executors: [{}] }, | ||
|
||
String: { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,19 @@ | ||
import { YveBot } from './core/bot'; | ||
|
||
export interface YveBotOptions { | ||
enableWaitForSleep?: boolean; | ||
rule?: Rule; | ||
} | ||
|
||
export interface RuleType { | ||
executors: RuleTypeExecutor[]; | ||
} | ||
|
||
export interface RuleTypeExecutor { | ||
validators?: RuleValidator[]; | ||
transform?: (value: any, rule?: Rule, bot?: YveBot) => Promise<any>; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. create a type |
||
} | ||
|
||
export interface Rule { | ||
name?: string; | ||
type?: string; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Exceptions instead of any