Add example of stopping an input modifier

This commit is contained in:
Andrew Cantino 2020-09-28 15:32:05 -07:00
parent ddf4d627c0
commit 7e26f0d756
2 changed files with 28 additions and 1 deletions

View file

@ -27,7 +27,8 @@ As an example, if you set `state.memory.authorsNote` to `the following paragraph
You can modify the quests property to change the quests of the adventure mid game.
### Input Modifier
Called each time the player gives an input and has the opportunity to modify that input.
Called each time the player gives an input and has the opportunity to modify that input. When inside of an Input Modifier,
you can return `stop: true` in order to stop processing——see [examples/commandParser.js].
### Context Modifier
Called each time the AI model is about to receive input and has the opportunity to modify that input (by up to a 75% [edit distance](https://en.wikipedia.org/wiki/Levenshtein_distance) change).

26
examples/commandParser.js Normal file
View file

@ -0,0 +1,26 @@
// This is an example Input Modifier that looks for commands from the user.
const modifier = (text) => {
let stop = false
// This matches when the user types in ":something arg1 arg2" in any of the three input formats. For example, they could
// type ":status" and then command would be "status" and args would be [], or they could type ":walk north" and command
// would be "walk" and args would be ["north"].
const commandMatcher = text.match(/\n? ?(?:> You |> You say "|):(\w+?)( [\w ]+)?[".]?\n?$/i)
if (commandMatcher) {
const command = commandMatcher[1]
const args = commandMatcher[2] ? commandMatcher[2].trim().split(' ') : []
state.message = `Got command '${command}' with args ${JSON.stringify(args)}`
stop = true
} else {
delete state.message
}
// You must return an object with the text property defined.
// If you include { stop: true } when inside of an input modifier, processing will be stopped and nothing will be
// sent to the AI.
return { text, stop }
}
// Don't modify this part
modifier(text)