diff --git a/CHANGES.md b/CHANGES.md index 93ac4f5..5bf8f3f 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,3 +1,13 @@ +## v3.2 + +Updating action parameters + +### breaking + +- the `merge` action now takes 4 parameters: `session_id`, `context`, `entities`, `msg` +- the `error` action now takes `context` as second parameter +- custom actions now take 2 parameters: `session_id`, `context` + ## v3.1 - allows for custom logging diff --git a/README.md b/README.md index 90b36fd..1e61567 100644 --- a/README.md +++ b/README.md @@ -45,16 +45,17 @@ actions = { :say => -> (session_id, msg) { p msg }, - :merge => -> (context, entities) { + :merge => -> (session_id, context, entities, msg) { return context }, - :error => -> (session_id, msg) { + :error => -> (session_id, context) { p 'Oops I don\'t know what to do.' }, } ``` -A custom action takes one parameter: +A custom action takes the following parameters: +* `session_id` - a unique identifier describing the user session * `context` - the `Hash` representing the session state Example: diff --git a/examples/joke.rb b/examples/joke.rb index 58275b0..1615fc4 100644 --- a/examples/joke.rb +++ b/examples/joke.rb @@ -30,7 +30,7 @@ def first_entity_value(entities, entity) :say => -> (session_id, msg) { p msg }, - :merge => -> (context, entities) { + :merge => -> (session_id, context, entities, msg) { new_context = context.clone new_context.delete 'joke' new_context.delete 'ack' @@ -40,10 +40,10 @@ def first_entity_value(entities, entity) new_context['ack'] = sentiment == 'positive' ? 'Glad you liked it.' : 'Hmm.' unless sentiment.nil? return new_context }, - :error => -> (session_id, msg) { + :error => -> (session_id, context) { p 'Oops I don\'t know what to do.' }, - :'select-joke' => -> (context) { + :'select-joke' => -> (session_id, context) { new_context = context.clone new_context['joke'] = all_jokes[new_context['cat'] || 'default'].sample return new_context diff --git a/examples/quickstart.rb b/examples/quickstart.rb index b509d21..e3b4364 100644 --- a/examples/quickstart.rb +++ b/examples/quickstart.rb @@ -16,16 +16,16 @@ def first_entity_value(entities, entity) :say => -> (session_id, msg) { p msg }, - :merge => -> (context, entities) { + :merge => -> (session_id, context, entities, msg) { new_context = context.clone loc = first_entity_value entities, 'location' new_context['loc'] = loc unless loc.nil? return new_context }, - :error => -> (session_id, msg) { + :error => -> (session_id, context) { p 'Oops I don\'t know what to do.' }, - :'fetch-weather' => -> (context) { + :'fetch-weather' => -> (session_id, context) { new_context = context.clone new_context['forecast'] = 'sunny' return new_context diff --git a/examples/template.rb b/examples/template.rb index d031524..051c7f4 100644 --- a/examples/template.rb +++ b/examples/template.rb @@ -6,10 +6,10 @@ :say => -> (session_id, msg) { p msg }, - :merge => -> (context, entities) { + :merge => -> (session_id, context, entities, msg) { return context }, - :error => -> (session_id, msg) { + :error => -> (session_id, context) { p 'Oops I don\'t know what to do.' }, } diff --git a/lib/wit.rb b/lib/wit.rb index 96301a5..ebcba34 100644 --- a/lib/wit.rb +++ b/lib/wit.rb @@ -37,9 +37,9 @@ def validate_actions(actions) raise WitException.new "The '#{k}' action name should be a symbol" unless k.is_a? Symbol raise WitException.new "The '#{k}' action should be a lambda function" unless v.respond_to? :call and v.lambda? raise WitException.new "The \'say\' action should take 2 arguments: session_id, msg. #{learn_more}" if k == :say and v.arity != 2 - raise WitException.new "The \'merge\' action should take 2 arguments: context, entities. #{learn_more}" if k == :merge and v.arity != 2 - raise WitException.new "The \'error\' action should take 2 arguments: session_id, msg. #{learn_more}" if k == :error and v.arity != 2 - raise WitException.new "The '#{k}' action should take 1 argument: context. #{learn_more}" if k != :say and k != :merge and k != :error and v.arity != 1 + raise WitException.new "The \'merge\' action should take 4 arguments: session_id, context, entities, msg. #{learn_more}" if k == :merge and v.arity != 4 + raise WitException.new "The \'error\' action should take 2 arguments: session_id, context. #{learn_more}" if k == :error and v.arity != 2 + raise WitException.new "The '#{k}' action should take 2 arguments: session_id, context. #{learn_more}" if k != :say and k != :merge and k != :error and v.arity != 2 end return actions end @@ -78,7 +78,7 @@ def converse(session_id, msg, context={}) req @access_token, Net::HTTP::Post, '/converse', params, context end - def run_actions(session_id, message, context={}, max_steps=DEFAULT_MAX_STEPS) + def run_actions_(session_id, message, context, max_steps, user_message) raise WitException.new 'max iterations reached' unless max_steps > 0 rst = converse session_id, message, context @@ -95,7 +95,7 @@ def run_actions(session_id, message, context={}, max_steps=DEFAULT_MAX_STEPS) elsif type == 'merge' raise WitException.new 'unknown action: merge' unless @actions.has_key? :merge logger.info 'Executing merge' - context = @actions[:merge].call context, rst['entities'] + context = @actions[:merge].call session_id, context, rst['entities'], user_message if context.nil? logger.warn 'missing context - did you forget to return it?' context = {} @@ -104,7 +104,7 @@ def run_actions(session_id, message, context={}, max_steps=DEFAULT_MAX_STEPS) action = rst['action'].to_sym raise WitException.new "unknown action: #{action}" unless @actions.has_key? action logger.info "Executing action #{action}" - context = @actions[action].call context + context = @actions[action].call session_id, context if context.nil? logger.warn 'missing context - did you forget to return it?' context = {} @@ -112,10 +112,16 @@ def run_actions(session_id, message, context={}, max_steps=DEFAULT_MAX_STEPS) elsif type == 'error' raise WitException.new 'unknown action: error' unless @actions.has_key? :error logger.info 'Executing error' - @actions[:error].call session_id, 'unknown action: error' + @actions[:error].call session_id, context else raise WitException.new "unknown type: #{type}" end return run_actions session_id, nil, context, max_steps - 1 end + + def run_actions(session_id, message, context={}, max_steps=DEFAULT_MAX_STEPS) + return run_actions_ session_id, message, context, max_steps, message + end + + private :run_actions_ end diff --git a/wit.gemspec b/wit.gemspec index ee7804a..2a924fc 100644 --- a/wit.gemspec +++ b/wit.gemspec @@ -1,7 +1,7 @@ Gem::Specification.new do |s| s.name = 'wit' - s.version = '3.1.0' - s.date = '2014-12-05' + s.version = '3.2.0' + s.date = Date.today.to_s s.summary = 'Ruby SDK for Wit.ai' s.description = 'Ruby SDK for Wit.ai' s.authors = ['The Wit Team']