aichaos / rivescript-js

A RiveScript interpreter for JavaScript. RiveScript is a scripting language for chatterbots.
https://www.rivescript.com/
MIT License
377 stars 145 forks source link

How to update trigger an reply dynamic? #399

Open cage1618 opened 1 year ago

cage1618 commented 1 year ago

rivescript is greate!

ATT, I found the stream method, but it seems only append new trigger. I want to update a trigger an reply when the bot is running but not stop it. Thanks

kirsle commented 1 year ago

This is currently not supported easily but I'd be open for a pull request that would fix this.

A long time ago it used to be possible - RiveScript loaded triggers in as a hashmap keyed by the trigger text, so seeing a 'duplicate' trigger would update/merge the data and reloading a bot's replies was possible without needing to fully re-initialize a RiveScript instance from scratch. But this caused some issues with the way %Previous was handled; having two 'identical' triggers that had a different %Previous on each, they'd end up overwriting each other by the trigger text and causing problems; so the module was refactored to load triggers into an array rather than a hash map.

The plus side was, 'identical' triggers having different %Previous commands worked since they wouldn't overwrite each other; but attempting to 'reload' triggers just appended the duplicates further down the array where they were overshadowed by the first version that was loaded the first time.

A fix for this may look like (probably added to the sortReplies() function): to de-duplicate or merge triggers having the same +Trigger and %Previous value.

cage1618 commented 1 year ago

Thanks for your reply, so currently the best practices is stop the bot and then reload it, am i right?

kirsle commented 1 year ago

Yeah - specifically, null out your old RiveScript object and replace it w/ a new RiveScript(); where you set up your bot anew.

This may reset user variables in memory though (if you're using the default settings); to make sure recent user variables cross over you can use getUservars()/setUservars().

// get the variables out of your current bot
let vars = await old_bot.getUservars();

let new_bot = new RiveScript();
await new_bot.loadDirectory("replies").then(new_bot.sortReplies);

// set them in the new bot
await new_bot.setUservars(vars);

// use the new bot (replace w/e you store RiveScript in
// with the new variable; the old bot will be garbage collected
// and future replies in your app call the new one now
old_bot = new_bot;
cage1618 commented 1 year ago

Ok, In this case, I have 2 questions:

  1. How about the input and reply tag reference? I thinks it will be lost in the new bot, am I right?
  2. How to release resources of the old bot?