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

Macro calls are processed separately, and after, set & get calls - leading to strange side effects #376

Closed lee-orr closed 2 years ago

lee-orr commented 2 years ago

Given the following case:

> object add_hello javascript
return "hello:" + args[0];
< object
> object get_value javascript
return rs.getUservar(rs.currentUser(), 'value');
< object
+ *
- <set value=<call>add_hello <star></call>> <call>get_value</call>

I'd expect, if a user said "test", the bot to respond with "hello:test". Instead, I get the response: "«call»add_hello test«/call»"

Similarly, in the following case:

> object add_hello javascript
return "hello:" + args[0];
< object
> object get_value javascript
return rs.getUservar(rs.currentUser(), 'value');
< object
+ set *
- <set value=<call>add_hello <star></call>>

+ *
- <call>get_value</call>

I'd expect that if a user said "set test", the bot would respond with "hello:test" if I say anything that doesn't match the first branch. Instead, I get the same response as above.

Lastly, with the following case:

> object add_initial javascript
return (await rs.getUservar(rs.currentUser(), 'initial')) + ":" + args[0];
< object
> object get_value javascript
return rs.getUservar(rs.currentUser(), 'value');
< object

+ set *
- <set value=<call>add_initial <star></call>> Set initialed value

+ initial *
- <set initial=<star>> Initial set to <star>

+ get
- <get value>

If the user said: "initial hello", "set test" and then "initial goodbye", I'd expect the response to "get" to be "hello:test", but it will be "goodbye:test" instead, since the call will be executed on the get, not at the set.

lee-orr commented 2 years ago

Note - upon further inspection I think #375 is a symptom of the same cause, so I'll close that issue and add the info here:

I have run into a few errors with nested object calls - where the result of any inner calls is an "Object not found" error.

Here is a minimal reproducing case:

> object wrapper javascript
return "_" + args[0] + "_";
< object

> object add_hello javascript
return "hello:" + args[0];
< object

+ works *
- <call>wrapper <star></call>

+ object not found *
- <call>wrapper <call>add_hello <star> </call></call>