From 7e26f0d7563f96700fee0f9cb59d64d62cbd2ed9 Mon Sep 17 00:00:00 2001 From: Andrew Cantino Date: Mon, 28 Sep 2020 15:32:05 -0700 Subject: [PATCH] Add example of stopping an input modifier --- README.md | 3 ++- examples/commandParser.js | 26 ++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 examples/commandParser.js diff --git a/README.md b/README.md index 046237d..bb8a658 100644 --- a/README.md +++ b/README.md @@ -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). diff --git a/examples/commandParser.js b/examples/commandParser.js new file mode 100644 index 0000000..6adfa3e --- /dev/null +++ b/examples/commandParser.js @@ -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)