Skip to content

Commit 1245d3b

Browse files
authored
Merge pull request #427 from hellostealth/feature/3.0-mountable-locals
Added locals logic
2 parents c888055 + 2ca6354 commit 1245d3b

File tree

5 files changed

+49
-113
lines changed

5 files changed

+49
-113
lines changed

app/controllers/stealth/controller.rb

Lines changed: 17 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,18 @@ class Controller < ApplicationController
44
include Stealth::Controller::InterruptDetect
55
include Stealth::Controller::DevJumps
66
include Stealth::Controller::Replies
7-
include Stealth::Controller::IntentClassifier
7+
include Stealth::Controller::IntentClassifier
88

99
attr_reader :current_message, :current_service, :current_session_id
10-
attr_accessor :nlp_result, :pos
10+
attr_accessor :nlp_result, :pos, :current_session, :previous_session
1111

1212
def initialize(service_event:, pos: nil)
1313
super()
1414
@current_message = service_event
1515
@current_service = service_event.service
1616
@current_session_id = service_event.sender_id
17-
# @nlp_result = service_event.nlp_result
17+
@current_session = Stealth::Session.new(id: current_session_id)
18+
@previous_session = Stealth::Session.new(id: current_session_id, type: :previous)
1819
@pos = pos
1920
@progressed = false
2021
end
@@ -31,72 +32,6 @@ def progressed?
3132
@progressed
3233
end
3334

34-
# def flow_controller
35-
# @flow_controller ||= begin
36-
# flow_controller = [current_session.flow_string, 'controller'].join('_').classify.constantize
37-
# flow_controller.new(service_message: @current_message, pos: @pos)
38-
# end
39-
# end
40-
41-
def current_session
42-
@current_session ||= Stealth::Session.new(id: current_session_id)
43-
end
44-
45-
def previous_session
46-
@previous_session ||= Stealth::Session.new(
47-
id: current_session_id,
48-
type: :previous
49-
)
50-
end
51-
52-
# def action(action: nil)
53-
# begin
54-
# # Grab a mutual exclusion lock on the session
55-
# lock_session!(
56-
# session_slug: Session.slugify(
57-
# flow: current_session.flow_string,
58-
# state: current_session.state_string
59-
# )
60-
# )
61-
62-
# @action_name = action
63-
# @action_name ||= current_session.state_string
64-
65-
# # Check if the user needs to be redirected
66-
# if current_session.flow.current_state.redirects_to.present?
67-
# Stealth::Logger.l(
68-
# topic: "redirect",
69-
# message: "From #{current_session.session} to #{current_session.flow.current_state.redirects_to.session}"
70-
# )
71-
# step_to(session: current_session.flow.current_state.redirects_to, pos: @pos)
72-
# return
73-
# end
74-
75-
# run_callbacks :action do
76-
# begin
77-
# flow_controller.send(@action_name)
78-
# unless flow_controller.progressed?
79-
# run_catch_all(reason: 'Did not send replies, update session, or step')
80-
# end
81-
# rescue Stealth::Errors::Halted
82-
# Stealth::Logger.l(
83-
# topic: "session",
84-
# message: "User #{current_session_id}: session halted."
85-
# )
86-
# rescue StandardError => e
87-
# if e.class == Stealth::Errors::UnrecognizedMessage
88-
# run_unrecognized_message(err: e)
89-
# else
90-
# run_catch_all(err: e)
91-
# end
92-
# end
93-
# end
94-
# ensure
95-
# # Release mutual exclusion lock on the session
96-
# release_lock!
97-
# end
98-
# end
99-
10035
def step_to_in(delay, session: nil, flow: nil, state: nil, slug: nil)
10136
if interrupt_detected?
10237
run_interrupt_action
@@ -151,7 +86,13 @@ def step_to(session: nil, flow: nil, state: nil, slug: nil, pos: nil, locals: ni
15186
state: state,
15287
slug: slug
15388
)
154-
step(flow: flow, state: state, pos: pos, locals: locals)
89+
current_session.locals = locals
90+
91+
# Workaround for update_session_to.
92+
if previous_session.before_update_session_to_locals.present?
93+
current_session.locals = previous_session.before_update_session_to_locals
94+
end
95+
step(flow: flow, state: state, pos: pos)
15596
end
15697

15798
def update_session_to(session: nil, flow: nil, state: nil, slug: nil, locals: nil)
@@ -166,8 +107,8 @@ def update_session_to(session: nil, flow: nil, state: nil, slug: nil, locals: ni
166107
state: state,
167108
slug: slug
168109
)
169-
170-
update_session(flow: flow, state: state, locals: locals)
110+
current_session.before_update_session_to_locals = locals
111+
update_session(flow: flow, state: state)
171112
end
172113

173114
def set_back_to(session: nil, flow: nil, state: nil, slug: nil)
@@ -212,15 +153,10 @@ def halt!
212153

213154
private
214155

215-
def update_session(flow:, state:, locals: nil)
156+
def update_session(flow:, state:)
216157
@progressed = :updated_session
217-
@current_session = Session.new(id: current_session_id)
218-
219-
unless current_session.flow_string == flow.to_s && current_session.state_string == state.to_s
220-
@current_session.locals = locals
221-
@current_session.set_session(new_flow: flow, new_state: state)
222-
end
223158

159+
current_session.set_session(new_flow: flow, new_state: state)
224160
end
225161

226162
def store_back_to_session(flow:, state:)
@@ -231,16 +167,12 @@ def store_back_to_session(flow:, state:)
231167
back_to_session.set_session(new_flow: flow, new_state: state)
232168
end
233169

234-
def step(flow:, state:, pos: nil, locals: nil)
235-
update_session(flow: flow, state: state, locals: locals)
170+
def step(flow:, state:, pos: nil)
171+
update_session(flow: flow, state: state)
236172
Stealth.trigger_flow(flow, state, @current_message)
237173

238174
@progressed = :stepped
239-
# @flow_controller = nil
240-
# @current_flow = current_session.flow
241175
@pos = pos
242-
243-
# flow_controller.action(action: state)
244176
end
245177

246178
def get_flow_and_state(session: nil, flow: nil, state: nil, slug: nil)
@@ -258,10 +190,6 @@ def get_flow_and_state(session: nil, flow: nil, state: nil, slug: nil)
258190
end
259191

260192
if flow.present?
261-
# Deprecated
262-
# if state.blank?
263-
# state = FlowMap.flow_spec[flow.to_sym].states.keys.first.to_s
264-
# end
265193

266194
if state.blank?
267195
# Access the existing FlowManager instance that has the registered flows

app/controllers/stealth/controller/replies.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def send_replies
7676
Stealth.trigger_reply(flow, state, current_message)
7777
end
7878

79-
def say(reply)
79+
def say(reply, thread_id: nil)
8080
reply_instance = Stealth::Reply.new(unstructured_reply: reply)
8181

8282
handler = reply_handler.new(
@@ -86,7 +86,7 @@ def say(reply)
8686

8787
formatted_reply = handler.send(reply_instance.reply_type)
8888

89-
client = service_client.new(reply: formatted_reply)
89+
client = service_client.new(reply: formatted_reply, thread_id: thread_id)
9090
client.transmit
9191
end
9292

app/spectre/prompts/intent_classifier/system.yml.erb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ system: |
99
<%= "Examples: #{intent[:examples]}" %>
1010
<% end %>
1111
If the intent is not in the list, reply with nothing.
12+
If the user's message is ambiguous or does not match the action and context of an intent, reply with nothing.

lib/stealth/event_mapping.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ class EventMapping
44
# slack-specific event mappings
55
'slack' => {
66
'text_received' => { event_type: :slack, event: :receive },
7+
'text_changed' => { event_type: :slack, event: :text_changed },
78
'reaction_received' => { event_type: :slack, event: :reaction },
8-
'interactive_response_received' => { event_type: :slack, event: :interactive_response }
9+
'interactive_response_received' => { event_type: :slack, event: :interactive_response },
10+
'assistant_thread_started' => { event_type: :slack, event: :assistant_thread_started },
911
}
1012
}.freeze
1113

lib/stealth/session.rb

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class Session
99
SLUG_SEPARATOR = '->'
1010

1111
attr_reader :flow, :state, :id, :type
12-
attr_accessor :session, :locals
12+
attr_accessor :session, :locals, :before_update_session_to_locals
1313

1414
# Session types:
1515
# - :primary
@@ -28,6 +28,7 @@ def initialize(id: nil, type: :primary)
2828
end
2929

3030
load_previous_locals
31+
load_before_update_session_to_locals
3132
get_session
3233
end
3334

@@ -68,17 +69,25 @@ def state_string
6869
end
6970

7071
def load_previous_locals
72+
load_json_from_redis(previous_locals_key, :@locals)
73+
end
74+
75+
def load_before_update_session_to_locals
76+
load_json_from_redis(before_update_session_to_locals_key, :@before_update_session_to_locals)
77+
end
78+
79+
def load_json_from_redis(key, instance_variable_name)
7180
return if primary_session?
7281

73-
@locals = get_key(previous_locals_key)
82+
value = get_key(key)
7483

75-
if @locals.present? && @locals.is_a?(String)
84+
if value.present? && value.is_a?(String)
7685
begin
77-
@locals = JSON.parse(@locals)
86+
instance_variable_set(instance_variable_name, JSON.parse(value))
7887
rescue JSON::ParserError => e
7988
Stealth::Logger.l(
8089
topic: "session",
81-
message: "User #{id}: failed to parse locals from Redis -> #{@locals}, error: #{e.message}"
90+
message: "User #{id}: failed to parse locals from Redis -> #{value}, error: #{e.message}"
8291
)
8392
end
8493
end
@@ -197,27 +206,23 @@ def previous_locals_key
197206
[id, 'previous', 'locals'].join('-')
198207
end
199208

209+
def before_update_session_to_locals_key
210+
[id, 'before_update_session_to', 'locals'].join('-')
211+
end
212+
200213
def back_to_key
201214
[id, 'back_to'].join('-')
202215
end
203216

204217
def store_current_to_previous(existing_session:)
205-
# Prevent previous_session from becoming current_session
206-
if session == existing_session
207-
Stealth::Logger.l(
208-
topic: "previous_session",
209-
message: "User #{id}: skipping setting to #{session}"\
210-
' because it is the same as current_session'
211-
)
212-
else
213-
Stealth::Logger.l(
214-
topic: "previous_session",
215-
message: "User #{id}: setting to #{existing_session}"
216-
)
217-
218-
persist_key(key: previous_session_key, value: existing_session)
219-
persist_key(key: previous_locals_key, value: @locals.to_json)
220-
end
218+
Stealth::Logger.l(
219+
topic: "previous_session",
220+
message: "User #{id}: setting to #{existing_session}"
221+
)
222+
223+
persist_key(key: previous_session_key, value: existing_session)
224+
persist_key(key: previous_locals_key, value: @locals.to_json)
225+
persist_key(key: before_update_session_to_locals_key, value: @before_update_session_to_locals.to_json)
221226
end
222227

223228
end

0 commit comments

Comments
 (0)