Skip to content

Commit 33d6a4b

Browse files
committed
v3.3.0 - unifying action parameters
1 parent c25ac22 commit 33d6a4b

File tree

6 files changed

+36
-31
lines changed

6 files changed

+36
-31
lines changed

CHANGES.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
## v3.3
2+
3+
Unifying action parameters
4+
5+
### breaking
6+
7+
- the `say` action now takes 3 parameters: `session_id`, `context`, `msg`
8+
- the `error` action now takes 3 parameters: `session_id`, `context`, `error`
9+
110
## v3.2
211

312
Updating action parameters

examples/joke.rb

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,26 +27,24 @@ def first_entity_value(entities, entity)
2727
}
2828

2929
actions = {
30-
:say => -> (session_id, msg) {
30+
:say => -> (session_id, context, msg) {
3131
p msg
3232
},
3333
:merge => -> (session_id, context, entities, msg) {
34-
new_context = context.clone
35-
new_context.delete 'joke'
36-
new_context.delete 'ack'
34+
context.delete 'joke'
35+
context.delete 'ack'
3736
category = first_entity_value entities, 'category'
38-
new_context['category'] = category unless category.nil?
37+
context['category'] = category unless category.nil?
3938
sentiment = first_entity_value entities, 'sentiment'
40-
new_context['ack'] = sentiment == 'positive' ? 'Glad you liked it.' : 'Hmm.' unless sentiment.nil?
41-
return new_context
39+
context['ack'] = sentiment == 'positive' ? 'Glad you liked it.' : 'Hmm.' unless sentiment.nil?
40+
return context
4241
},
43-
:error => -> (session_id, context) {
44-
p 'Oops I don\'t know what to do.'
42+
:error => -> (session_id, context, error) {
43+
p error.message
4544
},
4645
:'select-joke' => -> (session_id, context) {
47-
new_context = context.clone
48-
new_context['joke'] = all_jokes[new_context['cat'] || 'default'].sample
49-
return new_context
46+
context['joke'] = all_jokes[context['cat'] || 'default'].sample
47+
return context
5048
},
5149
}
5250
client = Wit.new access_token, actions

examples/quickstart.rb

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,20 @@ def first_entity_value(entities, entity)
1313
end
1414

1515
actions = {
16-
:say => -> (session_id, msg) {
16+
:say => -> (session_id, context, msg) {
1717
p msg
1818
},
1919
:merge => -> (session_id, context, entities, msg) {
20-
new_context = context.clone
2120
loc = first_entity_value entities, 'location'
22-
new_context['loc'] = loc unless loc.nil?
23-
return new_context
21+
context['loc'] = loc unless loc.nil?
22+
return context
2423
},
25-
:error => -> (session_id, context) {
26-
p 'Oops I don\'t know what to do.'
24+
:error => -> (session_id, context, error) {
25+
p error.message
2726
},
2827
:'fetch-weather' => -> (session_id, context) {
29-
new_context = context.clone
30-
new_context['forecast'] = 'sunny'
31-
return new_context
28+
context['forecast'] = 'sunny'
29+
return context
3230
},
3331
}
3432
client = Wit.new access_token, actions

examples/template.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
access_token = 'YOUR_ACCESS_TOKEN'
44

55
actions = {
6-
:say => -> (session_id, msg) {
6+
:say => -> (session_id, context, msg) {
77
p msg
88
},
99
:merge => -> (session_id, context, entities, msg) {
1010
return context
1111
},
12-
:error => -> (session_id, context) {
13-
p 'Oops I don\'t know what to do.'
12+
:error => -> (session_id, context, error) {
13+
p error.message
1414
},
1515
}
1616
client = Wit.new access_token, actions

lib/wit.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ def validate_actions(actions)
3636
actions.each_pair do |k, v|
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?
39-
raise WitException.new "The \'say\' action should take 2 arguments: session_id, msg. #{learn_more}" if k == :say and v.arity != 2
39+
raise WitException.new "The \'say\' action should take 3 arguments: session_id, context, msg. #{learn_more}" if k == :say and v.arity != 3
4040
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
41+
raise WitException.new "The \'error\' action should take 3 arguments: session_id, context, error. #{learn_more}" if k == :error and v.arity != 3
4242
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
@@ -91,11 +91,11 @@ def run_actions_(session_id, message, context, max_steps, user_message)
9191
raise WitException.new 'unknown action: say' unless @actions.has_key? :say
9292
msg = rst['msg']
9393
logger.info "Executing say with: #{msg}"
94-
@actions[:say].call session_id, msg
94+
@actions[:say].call session_id, context.clone, msg
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 session_id, context, rst['entities'], user_message
98+
context = @actions[:merge].call session_id, context.clone, rst['entities'], user_message
9999
if context.nil?
100100
logger.warn 'missing context - did you forget to return it?'
101101
context = {}
@@ -104,15 +104,15 @@ def run_actions_(session_id, message, context, max_steps, user_message)
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 session_id, context
107+
context = @actions[action].call session_id, context.clone
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, context
115+
@actions[:error].call session_id, context.clone, WitException.new('Oops, I don\'t know what to do.')
116116
else
117117
raise WitException.new "unknown type: #{type}"
118118
end

wit.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Gem::Specification.new do |s|
22
s.name = 'wit'
3-
s.version = '3.2.0'
3+
s.version = '3.3.0'
44
s.date = Date.today.to_s
55
s.summary = 'Ruby SDK for Wit.ai'
66
s.description = 'Ruby SDK for Wit.ai'

0 commit comments

Comments
 (0)