Class: Smith::Workflow::Router

Inherits:
Object
  • Object
show all
Defined in:
lib/smith/workflow/router.rb

Class Method Summary collapse

Class Method Details

.normalize_output(output) ⇒ Object



14
15
16
17
18
# File 'lib/smith/workflow/router.rb', line 14

def self.normalize_output(output)
  return output unless output.is_a?(Hash)

  output.transform_keys { |key| key.is_a?(String) ? key.to_sym : key }
end

.normalize_route_key(route) ⇒ Object



53
54
55
# File 'lib/smith/workflow/router.rb', line 53

def self.normalize_route_key(route)
  route.to_s.strip.to_sym
end

.resolve(classifier_output, config, workflow_class:) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/smith/workflow/router.rb', line 6

def self.resolve(classifier_output, config, workflow_class:)
  output = normalize_output(classifier_output)
  validate!(output, config)
  transition_name = select_transition(output, config)
  validate_transition_exists!(transition_name, workflow_class)
  transition_name
end

.select_transition(output, config) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/smith/workflow/router.rb', line 45

def self.select_transition(output, config)
  if output[:confidence] >= config[:confidence_threshold]
    config[:routes][normalize_route_key(output[:route])]
  else
    config[:fallback]
  end
end

.validate!(output, config) ⇒ Object



20
21
22
23
24
# File 'lib/smith/workflow/router.rb', line 20

def self.validate!(output, config)
  validate_structure!(output)
  validate_confidence!(output[:confidence])
  validate_route_key!(normalize_route_key(output[:route]), output[:confidence], config)
end

.validate_confidence!(confidence) ⇒ Object

Raises:



32
33
34
35
36
# File 'lib/smith/workflow/router.rb', line 32

def self.validate_confidence!(confidence)
  return if confidence.is_a?(Numeric) && confidence >= 0.0 && confidence <= 1.0

  raise WorkflowError, "router confidence must be a number in 0.0..1.0"
end

.validate_route_key!(route_key, confidence, config) ⇒ Object

Raises:



38
39
40
41
42
43
# File 'lib/smith/workflow/router.rb', line 38

def self.validate_route_key!(route_key, confidence, config)
  return if confidence < config[:confidence_threshold]
  return if config[:routes].key?(route_key)

  raise WorkflowError, "router route :#{route_key} not found in declared routes"
end

.validate_structure!(output) ⇒ Object

Raises:



26
27
28
29
30
# File 'lib/smith/workflow/router.rb', line 26

def self.validate_structure!(output)
  raise WorkflowError, "router classifier output must be a Hash" unless output.is_a?(Hash)
  raise WorkflowError, "router classifier output missing :route" unless output.key?(:route)
  raise WorkflowError, "router classifier output missing :confidence" unless output.key?(:confidence)
end

.validate_transition_exists!(transition_name, workflow_class) ⇒ Object

Raises:



57
58
59
60
61
# File 'lib/smith/workflow/router.rb', line 57

def self.validate_transition_exists!(transition_name, workflow_class)
  return if workflow_class.find_transition(transition_name)

  raise WorkflowError, "router selected transition :#{transition_name} which is not declared on the workflow"
end