Skip to content
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

Shell: Add option to watch/reload the brain #160

Merged
merged 9 commits into from
Sep 6, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
language: node_js
node_js:
- "5.2"
- "4.1"
- "4.0"
- "node"
- "6"
- "5"
- "4"
- "0.12"
- "0.11"
- "0.10"
- "iojs"
before_install:
- npm install -g grunt-cli coffee-script
Expand Down
2 changes: 1 addition & 1 deletion eg/brain/coffee.rive
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// RiveScript = require("./lib/rivescript")
// CoffeeObjectHandler = require("./lib/rivescript/lang/coffee")
// rs = new RiveScript()
// rs.setHandler("coffee", CoffeeObjectHandler)
// rs.setHandler("coffee", new CoffeeObjectHandler)

! version = 2.0

Expand Down
4 changes: 4 additions & 0 deletions rivescript.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ declare module "rivescript" {

clearUservars(user: string): void;

lastMatch(user: string): string;

initialMatch(user: string): string;

currentUser(): string;
}

Expand Down
131 changes: 80 additions & 51 deletions shell.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
******************************************************************************/

var readline = require("readline"),
fs = require("fs"),
RiveScript = require("./lib/rivescript");

//------------------------------------------------------------------------------
Expand All @@ -16,20 +17,21 @@ var readline = require("readline"),
var opts = {
debug: false,
utf8: false,
watch: false,
brain: undefined
};

process.argv.forEach(function(val, index, array) {
if (index < 2) {
return;
}

process.argv.slice(2).forEach(function(val, index, array) {

if (val === "--debug") {
opts.debug = true;
}
else if (val === "--utf8") {
opts.utf8 = true;
}
else if (val === "--watch") {
opts.watch = true;
}
else if (val.indexOf("-") === 0) {
console.error("Unknown option: %s", val);
}
Expand All @@ -42,69 +44,94 @@ process.argv.forEach(function(val, index, array) {
});

if (opts.brain === undefined) {
console.log("Usage: node shell.js [--debug --utf8] </path/to/brain>");
console.log("Usage: node shell.js [--debug --utf8 --watch] </path/to/brain>");
process.exit(1);
}

//------------------------------------------------------------------------------
// Initialize the RiveScript bot and print the welcome banner.
//------------------------------------------------------------------------------

var bot = new RiveScript({
debug: opts.debug,
utf8: opts.utf8,
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
bot.loadDirectory(opts.brain, loading_done, loading_error);

function loading_done(batch_num) {
bot.sortReplies();

console.log(" . . ");
console.log(" .:...:: RiveScript Interpreter (JavaScript)");
console.log(" .:: ::. Library Version: v" + bot.version());
console.log(" ..:;;. ' .;;:.. ");
console.log(" . ''' . Type '/quit' to quit.");
console.log(" :;,:,;: Type '/help' for more options.");
console.log(" : : ");
console.log("");
console.log("Using the RiveScript bot found in: " + opts.brain);
console.log("Type a message to the bot and press Return to send it.");
console.log("");

//--------------------------------------------------------------------------
// Drop into the interactive command shell.
//--------------------------------------------------------------------------
var bot = null;
function loadBot() {
bot = new RiveScript({
debug: opts.debug,
utf8: opts.utf8,
});
bot.ready = false;
bot.loadDirectory(opts.brain, loadingDone, loadingError);
}
loadBot();

var rl = readline.createInterface({
input: process.stdin,
output: process.stdout
if (opts.watch) {
fs.watch(opts.brain, {recursive: false}, function() {
console.log("");
console.log('[INFO] Brain changed, reloading bot.');
rl.prompt();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this to require the user to press enter? Is it necessary? I think most watcher things just notify of the reload and continue.

Copy link
Contributor Author

@julien-c julien-c Sep 2, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope it's just to display the > input sign again in the terminal's last line. Necessary as we've been writing to the console.log. No user action needed of any sort.

loadBot();
});
}

rl.setPrompt("You> ");
rl.prompt();
rl.on("line", function(cmd) {
// Handle commands.
if (cmd === "/help") {
help();
} else if (cmd.indexOf("/eval ") === 0) {
eval(cmd.replace("/eval ", ""));
} else if (cmd.indexOf("/log ") === 0) {
console.log(eval(cmd.replace("/log ", "")));
} else if (cmd === "/quit") {
process.exit(0);
} else {
// Get a reply from the bot.
var reply = bot.reply("localuser", cmd);
console.log("Bot>", reply);
}

rl.prompt();
}).on("close", function() {
//--------------------------------------------------------------------------
// Drop into the interactive command shell.
//--------------------------------------------------------------------------

console.log(" . . ");
console.log(" .:...:: RiveScript Interpreter (JavaScript)");
console.log(" .:: ::. Library Version: v" + bot.version());
console.log(" ..:;;. ' .;;:.. ");
console.log(" . ''' . Type '/quit' to quit.");
console.log(" :;,:,;: Type '/help' for more options.");
console.log(" : : ");
console.log("");
console.log("Using the RiveScript bot found in: " + opts.brain);
console.log("Type a message to the bot and press Return to send it.");
console.log("");


rl.setPrompt("You> ");
rl.prompt();
rl.on('line', function(cmd) {
// Handle commands.
if (cmd === "/help") {
help();
} else if (cmd.indexOf("/data") === 0) {
console.log(bot.getUservars("localuser"));
} else if (cmd.indexOf("/eval ") === 0) {
console.log(eval(cmd.replace("/eval ", "")));
} else if (cmd.indexOf("/log ") === 0) {
console.log(eval(cmd.replace("/log ", "")));
} else if (cmd === "/quit") {
process.exit(0);
});
} else {
// Get a reply from the bot.
var reply = (bot && bot.ready)
? bot.reply("localuser", cmd)
: "ERR: Bot Not Ready Yet";
console.log("Bot>", reply);
}

rl.prompt();
}).on('close', function() {
console.log("");
process.exit(0);
});



function loadingDone(batchNumber) {
bot.sortReplies();
bot.ready = true;
}

function loading_error(error, loadBatch) {
function loadingError(error, batchNumber) {
console.error("Loading error: " + error);
}

Expand All @@ -115,3 +142,5 @@ function help() {
console.log("/log <code> : Shortcut to /eval console.log(code).");
console.log("/quit : Exit the program.");
}


2 changes: 1 addition & 1 deletion src/lang/coffee.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ coffee = require "coffee-script"
#
# ```coffeescript
# CoffeeObjectHandler = require "rivescript/lang/coffee"
# bot.setHandler "coffee", CoffeeObjectHandler
# bot.setHandler "coffee", new CoffeeObjectHandler
# ```
##
class CoffeeObjectHandler
Expand Down