Class: FlowChat::BaseProcessor

Inherits:
Object
  • Object
show all
Includes:
Instrumentation
Defined in:
lib/flow_chat/base_processor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Instrumentation

#instrument, instrument

Constructor Details

#initialize(controller, enable_simulator: nil) {|_self| ... } ⇒ BaseProcessor

Returns a new instance of BaseProcessor.

Yields:

  • (_self)

Yield Parameters:



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/flow_chat/base_processor.rb', line 9

def initialize(controller, enable_simulator: nil)
  FlowChat.logger.debug { "BaseProcessor: Initializing processor for controller #{controller.class.name}" }

  @context = FlowChat::Context.new
  @context["controller"] = controller
  @context["enable_simulator"] = enable_simulator.nil? ? (defined?(Rails) && Rails.env.local?) : enable_simulator
  @middleware = ::Middleware::Builder.new(name: middleware_name)
  @session_options = FlowChat::Config.session

  FlowChat.logger.debug { "BaseProcessor: Simulator mode #{@context["enable_simulator"] ? "enabled" : "disabled"}" }

  yield self if block_given?

  FlowChat.logger.debug { "BaseProcessor: Initialized #{self.class.name} successfully" }
end

Instance Attribute Details

#middlewareObject (readonly)

Returns the value of attribute middleware.



7
8
9
# File 'lib/flow_chat/base_processor.rb', line 7

def middleware
  @middleware
end

Instance Method Details

#run(flow_class, action, **options) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/flow_chat/base_processor.rb', line 73

def run(flow_class, action, **options)
  # Instrument flow execution (this will log via LogSubscriber)
  instrument(Events::FLOW_EXECUTION_START, {
    flow_name: flow_class.name.underscore,
    action: action.to_s,
    processor_type: self.class.name
  })

  @context["flow.name"] = flow_class.name.underscore
  @context["flow.class"] = flow_class
  @context["flow.action"] = action
  @context["flow.options"] = options

  FlowChat.logger.debug { "BaseProcessor: Context prepared for flow #{flow_class.name}" }

  stack = build_middleware_stack
  yield stack if block_given?

  FlowChat.logger.debug { "BaseProcessor: Executing middleware stack for #{flow_class.name}##{action}" }

  # Instrument flow execution with timing (this will log completion via LogSubscriber)
  instrument(Events::FLOW_EXECUTION_END, {
    flow_name: flow_class.name.underscore,
    action: action.to_s,
    processor_type: self.class.name
  }) do
    stack.call(@context)
  end
rescue => error
  FlowChat.logger.error { "BaseProcessor: Flow execution failed - #{flow_class.name}##{action}, Error: #{error.class.name}: #{error.message}" }
  FlowChat.logger.debug { "BaseProcessor: Stack trace: #{error.backtrace.join("\n")}" }

  # Instrument flow execution error (this will log error via LogSubscriber)
  instrument(Events::FLOW_EXECUTION_ERROR, {
    flow_name: flow_class.name.underscore,
    action: action.to_s,
    processor_type: self.class.name,
    error_class: error.class.name,
    error_message: error.message,
    backtrace: error.backtrace&.first(10)
  })

  raise
end

#use_cross_platform_sessionsObject



58
59
60
61
62
63
64
# File 'lib/flow_chat/base_processor.rb', line 58

def use_cross_platform_sessions
  FlowChat.logger.debug { "BaseProcessor: Enabling cross-platform sessions via session configuration" }
  use_session_config(
    boundaries: [:flow]
  )
  self
end

#use_gateway(gateway_class, *args) ⇒ Object



25
26
27
28
29
30
# File 'lib/flow_chat/base_processor.rb', line 25

def use_gateway(gateway_class, *args)
  FlowChat.logger.debug { "BaseProcessor: Configuring gateway #{gateway_class.name} with args: #{args.inspect}" }
  @gateway_class = gateway_class
  @gateway_args = args
  self
end

#use_middleware(middleware) ⇒ Object



51
52
53
54
55
56
# File 'lib/flow_chat/base_processor.rb', line 51

def use_middleware(middleware)
  raise "Middleware must be a class" unless middleware.is_a?(Class)
  FlowChat.logger.debug { "BaseProcessor: Adding middleware #{middleware.name}" }
  @middleware.use middleware
  self
end

#use_session_config(boundaries: nil, hash_identifiers: nil, identifier: nil) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/flow_chat/base_processor.rb', line 39

def use_session_config(boundaries: nil, hash_identifiers: nil, identifier: nil)
  FlowChat.logger.debug { "BaseProcessor: Configuring session config: boundaries=#{boundaries.inspect}, hash_identifiers=#{hash_identifiers}, identifier=#{identifier}" }
  
  # Update the session options directly
  @session_options = @session_options.dup
  @session_options.boundaries = Array(boundaries) if boundaries != nil
  @session_options.hash_identifiers = hash_identifiers if hash_identifiers != nil
  @session_options.identifier = identifier if identifier != nil
  
  self
end

#use_session_store(session_store) ⇒ Object



32
33
34
35
36
37
# File 'lib/flow_chat/base_processor.rb', line 32

def use_session_store(session_store)
  raise "Session store must be a class" unless session_store.is_a?(Class)
  FlowChat.logger.debug { "BaseProcessor: Configuring session store #{session_store.name}" }
  @context["session.store"] = session_store
  self
end

#use_url_isolationObject



66
67
68
69
70
71
# File 'lib/flow_chat/base_processor.rb', line 66

def use_url_isolation
  FlowChat.logger.debug { "BaseProcessor: Enabling URL-based session isolation" }
  current_boundaries = @session_options.boundaries.dup
  current_boundaries << :url unless current_boundaries.include?(:url)
  use_session_config(boundaries: current_boundaries)
end