Skip to content

Commit c25ac22

Browse files
committed
v3.2 - updating action parameters
1 parent a19da9d commit c25ac22

File tree

7 files changed

+37
-20
lines changed

7 files changed

+37
-20
lines changed

CHANGES.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
## v3.2
2+
3+
Updating action parameters
4+
5+
### breaking
6+
7+
- the `merge` action now takes 4 parameters: `session_id`, `context`, `entities`, `msg`
8+
- the `error` action now takes `context` as second parameter
9+
- custom actions now take 2 parameters: `session_id`, `context`
10+
111
## v3.1
212

313
- allows for custom logging

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,17 @@ actions = {
4545
:say => -> (session_id, msg) {
4646
p msg
4747
},
48-
:merge => -> (context, entities) {
48+
:merge => -> (session_id, context, entities, msg) {
4949
return context
5050
},
51-
:error => -> (session_id, msg) {
51+
:error => -> (session_id, context) {
5252
p 'Oops I don\'t know what to do.'
5353
},
5454
}
5555
```
5656

57-
A custom action takes one parameter:
57+
A custom action takes the following parameters:
58+
* `session_id` - a unique identifier describing the user session
5859
* `context` - the `Hash` representing the session state
5960

6061
Example:

examples/joke.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def first_entity_value(entities, entity)
3030
:say => -> (session_id, msg) {
3131
p msg
3232
},
33-
:merge => -> (context, entities) {
33+
:merge => -> (session_id, context, entities, msg) {
3434
new_context = context.clone
3535
new_context.delete 'joke'
3636
new_context.delete 'ack'
@@ -40,10 +40,10 @@ def first_entity_value(entities, entity)
4040
new_context['ack'] = sentiment == 'positive' ? 'Glad you liked it.' : 'Hmm.' unless sentiment.nil?
4141
return new_context
4242
},
43-
:error => -> (session_id, msg) {
43+
:error => -> (session_id, context) {
4444
p 'Oops I don\'t know what to do.'
4545
},
46-
:'select-joke' => -> (context) {
46+
:'select-joke' => -> (session_id, context) {
4747
new_context = context.clone
4848
new_context['joke'] = all_jokes[new_context['cat'] || 'default'].sample
4949
return new_context

examples/quickstart.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,16 @@ def first_entity_value(entities, entity)
1616
:say => -> (session_id, msg) {
1717
p msg
1818
},
19-
:merge => -> (context, entities) {
19+
:merge => -> (session_id, context, entities, msg) {
2020
new_context = context.clone
2121
loc = first_entity_value entities, 'location'
2222
new_context['loc'] = loc unless loc.nil?
2323
return new_context
2424
},
25-
:error => -> (session_id, msg) {
25+
:error => -> (session_id, context) {
2626
p 'Oops I don\'t know what to do.'
2727
},
28-
:'fetch-weather' => -> (context) {
28+
:'fetch-weather' => -> (session_id, context) {
2929
new_context = context.clone
3030
new_context['forecast'] = 'sunny'
3131
return new_context

examples/template.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
:say => -> (session_id, msg) {
77
p msg
88
},
9-
:merge => -> (context, entities) {
9+
:merge => -> (session_id, context, entities, msg) {
1010
return context
1111
},
12-
:error => -> (session_id, msg) {
12+
:error => -> (session_id, context) {
1313
p 'Oops I don\'t know what to do.'
1414
},
1515
}

lib/wit.rb

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ def validate_actions(actions)
3737
raise WitException.new "The '#{k}' action name should be a symbol" unless k.is_a? Symbol
3838
raise WitException.new "The '#{k}' action should be a lambda function" unless v.respond_to? :call and v.lambda?
3939
raise WitException.new "The \'say\' action should take 2 arguments: session_id, msg. #{learn_more}" if k == :say and v.arity != 2
40-
raise WitException.new "The \'merge\' action should take 2 arguments: context, entities. #{learn_more}" if k == :merge and v.arity != 2
41-
raise WitException.new "The \'error\' action should take 2 arguments: session_id, msg. #{learn_more}" if k == :error and v.arity != 2
42-
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
40+
raise WitException.new "The \'merge\' action should take 4 arguments: session_id, context, entities, msg. #{learn_more}" if k == :merge and v.arity != 4
41+
raise WitException.new "The \'error\' action should take 2 arguments: session_id, context. #{learn_more}" if k == :error and v.arity != 2
42+
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
4343
end
4444
return actions
4545
end
@@ -78,7 +78,7 @@ def converse(session_id, msg, context={})
7878
req @access_token, Net::HTTP::Post, '/converse', params, context
7979
end
8080

81-
def run_actions(session_id, message, context={}, max_steps=DEFAULT_MAX_STEPS)
81+
def run_actions_(session_id, message, context, max_steps, user_message)
8282
raise WitException.new 'max iterations reached' unless max_steps > 0
8383

8484
rst = converse session_id, message, context
@@ -95,7 +95,7 @@ def run_actions(session_id, message, context={}, max_steps=DEFAULT_MAX_STEPS)
9595
elsif type == 'merge'
9696
raise WitException.new 'unknown action: merge' unless @actions.has_key? :merge
9797
logger.info 'Executing merge'
98-
context = @actions[:merge].call context, rst['entities']
98+
context = @actions[:merge].call session_id, context, rst['entities'], user_message
9999
if context.nil?
100100
logger.warn 'missing context - did you forget to return it?'
101101
context = {}
@@ -104,18 +104,24 @@ def run_actions(session_id, message, context={}, max_steps=DEFAULT_MAX_STEPS)
104104
action = rst['action'].to_sym
105105
raise WitException.new "unknown action: #{action}" unless @actions.has_key? action
106106
logger.info "Executing action #{action}"
107-
context = @actions[action].call context
107+
context = @actions[action].call session_id, context
108108
if context.nil?
109109
logger.warn 'missing context - did you forget to return it?'
110110
context = {}
111111
end
112112
elsif type == 'error'
113113
raise WitException.new 'unknown action: error' unless @actions.has_key? :error
114114
logger.info 'Executing error'
115-
@actions[:error].call session_id, 'unknown action: error'
115+
@actions[:error].call session_id, context
116116
else
117117
raise WitException.new "unknown type: #{type}"
118118
end
119119
return run_actions session_id, nil, context, max_steps - 1
120120
end
121+
122+
def run_actions(session_id, message, context={}, max_steps=DEFAULT_MAX_STEPS)
123+
return run_actions_ session_id, message, context, max_steps, message
124+
end
125+
126+
private :run_actions_
121127
end

wit.gemspec

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Gem::Specification.new do |s|
22
s.name = 'wit'
3-
s.version = '3.1.0'
4-
s.date = '2014-12-05'
3+
s.version = '3.2.0'
4+
s.date = Date.today.to_s
55
s.summary = 'Ruby SDK for Wit.ai'
66
s.description = 'Ruby SDK for Wit.ai'
77
s.authors = ['The Wit Team']

0 commit comments

Comments
 (0)