aws-lex-conversation 6.1.1 → 6.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f15c999ea65d5d749443fbed6e0871abe69ffa65d399febdd2900d5d99b9730c
4
- data.tar.gz: 21165febaf008f32900422e98dffa448dc6caefade98908c5dc6e0b32e97cab9
3
+ metadata.gz: 3a4ab092d321df0c71e5f16cf1021e87358b1b39ab025eaf5555e3e3da7894c1
4
+ data.tar.gz: a0332c4f2f73b35972c83a0740e1adfa4d724fe5d9861087d348e905e3e15633
5
5
  SHA512:
6
- metadata.gz: a6a2fbd9f33532e9e73bd130bd42983ebc1bc8398c9425ac9f8d82f12d55e941d8979bdc498341a85a99462f74b0de5a42afea61ad15422c0b0032d55d8fd207
7
- data.tar.gz: 929adca898e5825a5eb7b9261ad78647487a9d452e66d83c06035f3c0fc564ae01035f6df8143e201ea12db976144980c04d7b1f842deb60ccbc1b8fd7c537a1
6
+ metadata.gz: 734b1ec8921c8982a3e123520b08b0d0b9e655159c781f7df18d106b8de635815bc026862fd6a4ec7a3d2665d47393bb8d977cf530a1331e2f3c2bea543b4a94
7
+ data.tar.gz: 2c4c43bafd522791d6f5f39596c7b594d3788098876b51fc8294cf2304a23c42a06fd6635e15e791410bc824273599f9afab3935fd853981f81b732672d03d96
data/CHANGELOG.md CHANGED
@@ -1,3 +1,10 @@
1
+ # 6.2.0 - Sept 28, 2021
2
+
3
+ * Add a `Aws::Lex::Conversation#restore_from!` method that accepts a checkpoint parameter. This method modifies the underlying conversation state to match the data from the saved checkpoint.
4
+ * Make the `dialog_action_type` parameter on `Aws::Lex::Conversation#checkpoint!` default to `Delegate` if not specified as a developer convenience.
5
+ * Allow developers to pass an optional `intent` override parameter on `Aws::Lex::Conversation#checkpoint!` for convenience.
6
+ * Update the README with advanced examples for the conversation stash and checkpoints.
7
+
1
8
  # 6.1.1 - Sept 22, 2021
2
9
 
3
10
  * renamed `maximum_elicitations` to `max_retries` and made it backwards compatible to make the param name clear, by default this value is zero, allowing each slot to elicit only once
data/README.md CHANGED
@@ -191,6 +191,114 @@ conversation.handlers = [
191
191
  conversation.respond # => { dialogAction: { type: 'Delegate' } }
192
192
  ```
193
193
 
194
+ ## Advanced Concepts
195
+
196
+ This library provides a few constructs to help manage complex interactions:
197
+
198
+ ### Data Stash
199
+
200
+ `Aws::Lex::Conversation` instances implement a `stash` method that can be used to store temporary data within a single invocation.
201
+
202
+ A conversation's stashed data will not be persisted between multiple invocations of your lambda function.
203
+
204
+ The conversation stash is a great spot to store deserialized data from the session, or invocation-specific state that needs to be shared between handler classes.
205
+
206
+ This example illustrates how the stash can be used to store deserialized data from the session:
207
+
208
+ ```ruby
209
+ # given we have JSON-serialized data in as a persisted session value
210
+ conversation.session[:user_data] = '{"name":"Jane","id":1234,"email":"test@example.com"}'
211
+ # we can deserialize the data into a Hash that we store in the conversation stash
212
+ conversation.stash[:user] = JSON.parse(conversation.session[:user_data])
213
+ # later on we can reference our stashed data (within the same invocation)
214
+ conversation.stash[:user] # => {"name"=>"Jane", "id"=>1234, "email"=>"test@example.com"}
215
+ ```
216
+
217
+ ### Checkpoints
218
+
219
+ A conversation may transition between many different topics as the interaction progresses. This type of state transition can be easily handled with checkpoints.
220
+
221
+ When a checkpoint is created, all intent and slot data is encoded and stored into a `checkpoints` session value. This data persists between invocations, and is not removed until the checkpoint is restored.
222
+
223
+ You can create a checkpoint as follows:
224
+
225
+ ```ruby
226
+ # we're ready to fulfill the OrderFlowers intent, but we want to elicit another intent first
227
+ conversation.checkpoint!(
228
+ label: 'order_flowers',
229
+ dialog_action_type: 'Close' # defaults to 'Delegate' if not specified
230
+ )
231
+ conversation.elicit_intent(
232
+ messages: [
233
+ {
234
+ content: 'Thanks! Before I place your order, is there anything else I can help with?',
235
+ contentType: 'PlainText'
236
+ }
237
+ ]
238
+ )
239
+ ```
240
+
241
+ You can restore the checkpoint in one of two ways:
242
+
243
+ ```ruby
244
+ # in a future invocation, we can fetch an instance of the checkpoint and easily
245
+ # restore the conversation to the previous state
246
+ checkpoint = conversation.checkpoint(label: 'order_flowers')
247
+ checkpoint.restore!(
248
+ fulfillment_state: 'Fulfilled',
249
+ messages: [
250
+ {
251
+ content: 'Okay, your flowers have been ordered! Thanks!',
252
+ contentType: 'PlainText'
253
+ }
254
+ ]
255
+ ) # => our response object to Lex is returned
256
+ ```
257
+
258
+ It's also possible to restore state from a checkpoint and utilize the conversation's handler chain:
259
+
260
+ ```ruby
261
+ class AnotherIntent < Aws::Lex::Conversation::Handler::Base
262
+ def will_respond?(conversation)
263
+ conversation.intent_name == 'AnotherIntent' &&
264
+ conversation.checkpoint?(label: 'order_flowers')
265
+ end
266
+
267
+ def response(conversation)
268
+ checkpoint = conversation.checkpoint(label: 'order_flowers')
269
+ # replace the conversation's current resolved intent/slot data with the saved checkpoint data
270
+ conversation.restore_from!(checkpoint)
271
+ # call the next handler in the chain to produce a response
272
+ successor.handle(conversation)
273
+ end
274
+ end
275
+
276
+ class OrderFlowers < Aws::Lex::Conversation::Handler::Base
277
+ def will_respond?(conversation)
278
+ conversation.intent_name == 'OrderFlowers'
279
+ end
280
+
281
+ def response(conversation)
282
+ conversation.close(
283
+ fulfillment_state: 'Fulfilled',
284
+ messages: [
285
+ {
286
+ content: 'Okay, your flowers have been ordered! Thanks!',
287
+ contentType: 'PlainText'
288
+ }
289
+ ]
290
+ )
291
+ end
292
+ end
293
+
294
+ conversation = Aws::Lex::Conversation.new(event: event, context: context)
295
+ conversation.handlers = [
296
+ { handler: AnotherIntent },
297
+ { handler: OrderFlowers }
298
+ ]
299
+ conversation.respond # => returns a Lex response object
300
+ ```
301
+
194
302
  ## Test Helpers
195
303
 
196
304
  This library provides convenience methods to make testing easy! You can use the test helpers as follows:
@@ -20,6 +20,28 @@ module Aws
20
20
  fulfillment_state: FulfillmentState
21
21
  )
22
22
 
23
+ class << self
24
+ def build(opts = {})
25
+ new(normalize_parameters(opts))
26
+ end
27
+
28
+ private
29
+
30
+ def normalize_parameters(opts)
31
+ params = opts.dup # we don't want to mutate our arguments
32
+
33
+ if params[:dialog_action_type].is_a?(String)
34
+ params[:dialog_action_type] = DialogActionType.new(params[:dialog_action_type])
35
+ end
36
+
37
+ if params[:fulfillment_state].is_a?(String)
38
+ params[:fulfillment_state] = FulfillmentState.new(params[:fulfillment_state])
39
+ end
40
+
41
+ params
42
+ end
43
+ end
44
+
23
45
  # restore the checkpoint AND remove it from session
24
46
  def restore!(conversation, opts = {})
25
47
  conversation.checkpoints.delete_if { |c| c.label == label }
@@ -3,7 +3,7 @@
3
3
  module Aws
4
4
  module Lex
5
5
  class Conversation
6
- VERSION = '6.1.1'
6
+ VERSION = '6.2.0'
7
7
  end
8
8
  end
9
9
  end
@@ -62,9 +62,9 @@ module Aws
62
62
  label = opts.fetch(:label)
63
63
  params = {
64
64
  label: label,
65
- dialog_action_type: opts.fetch(:dialog_action_type),
65
+ dialog_action_type: opts.fetch(:dialog_action_type) { 'Delegate' },
66
66
  fulfillment_state: opts[:fulfillment_state],
67
- intent: lex.current_intent,
67
+ intent: opts.fetch(:intent) { lex.current_intent },
68
68
  slot_to_elicit: opts[:slot_to_elicit]
69
69
  }.compact
70
70
 
@@ -72,9 +72,8 @@ module Aws
72
72
  # update the existing checkpoint
73
73
  checkpoint(label: label).assign_attributes!(params)
74
74
  else
75
- # push a new checkpoint to the recent_intent_summary_view
76
75
  checkpoints.unshift(
77
- Type::Checkpoint.new(params)
76
+ Type::Checkpoint.build(params)
78
77
  )
79
78
  end
80
79
  end
@@ -91,6 +90,21 @@ module Aws
91
90
  lex.session_state.session_attributes.checkpoints
92
91
  end
93
92
 
93
+ def restore_from!(checkpoint)
94
+ # we're done with the stored checkpoint once it's been restored
95
+ checkpoints.delete_if { |c| c.label == checkpoint.label }
96
+ # remove any memoized intent data
97
+ lex.current_intent = nil
98
+ # replace the intent with data from the checkpoint
99
+ lex.session_state.intent = checkpoint.intent
100
+ dialog_action = Type::DialogAction.new(
101
+ type: checkpoint.dialog_action_type,
102
+ slot_to_elicit: checkpoint.slot_to_elicit
103
+ )
104
+ lex.session_state.dialog_action = dialog_action
105
+ self
106
+ end
107
+
94
108
  def active_context?(name:)
95
109
  !active_context(name: name).nil?
96
110
  end
@@ -104,7 +118,7 @@ module Aws
104
118
  instance = active_context(name: name)
105
119
 
106
120
  if instance
107
- lex.session_state.active_contexts.delete_if { |c| c.name == name }
121
+ clear_context!(name: name)
108
122
  else
109
123
  instance = Type::Context.new
110
124
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: aws-lex-conversation
3
3
  version: !ruby/object:Gem::Version
4
- version: 6.1.1
4
+ version: 6.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jesse Doyle
@@ -12,7 +12,7 @@ authors:
12
12
  autorequire:
13
13
  bindir: exe
14
14
  cert_chain: []
15
- date: 2021-09-22 00:00:00.000000000 Z
15
+ date: 2021-09-28 00:00:00.000000000 Z
16
16
  dependencies:
17
17
  - !ruby/object:Gem::Dependency
18
18
  name: shrink_wrap