Skip to content

Commit

Permalink
v3.2 - updating action parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
patapizza committed Apr 15, 2016
1 parent a19da9d commit c25ac22
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 20 deletions.
10 changes: 10 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
6 changes: 3 additions & 3 deletions examples/joke.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions examples/quickstart.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions examples/template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.'
},
}
Expand Down
20 changes: 13 additions & 7 deletions lib/wit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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 = {}
Expand All @@ -104,18 +104,24 @@ 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 = {}
end
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
4 changes: 2 additions & 2 deletions wit.gemspec
Original file line number Diff line number Diff line change
@@ -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']
Expand Down

0 comments on commit c25ac22

Please sign in to comment.