Class: Smith::Workflow::Transition

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, from:, to:) ⇒ Transition

Returns a new instance of Transition.



11
12
13
14
15
16
# File 'lib/smith/workflow/transition.rb', line 11

def initialize(name, from:, to:, &)
  @name = name
  @from = from
  @to = to
  instance_eval(&) if block_given?
end

Instance Attribute Details

#agent_nameObject (readonly)

Returns the value of attribute agent_name.



6
7
8
# File 'lib/smith/workflow/transition.rb', line 6

def agent_name
  @agent_name
end

#agent_optsObject (readonly)

Returns the value of attribute agent_opts.



6
7
8
# File 'lib/smith/workflow/transition.rb', line 6

def agent_opts
  @agent_opts
end

#deterministic_blockObject (readonly)

Returns the value of attribute deterministic_block.



6
7
8
# File 'lib/smith/workflow/transition.rb', line 6

def deterministic_block
  @deterministic_block
end

#deterministic_kindObject (readonly)

Returns the value of attribute deterministic_kind.



6
7
8
# File 'lib/smith/workflow/transition.rb', line 6

def deterministic_kind
  @deterministic_kind
end

#deterministic_routesObject (readonly)

Returns the value of attribute deterministic_routes.



6
7
8
# File 'lib/smith/workflow/transition.rb', line 6

def deterministic_routes
  @deterministic_routes
end

#failure_transitionObject (readonly)

Returns the value of attribute failure_transition.



6
7
8
# File 'lib/smith/workflow/transition.rb', line 6

def failure_transition
  @failure_transition
end

#fanout_configObject (readonly)

Returns the value of attribute fanout_config.



6
7
8
# File 'lib/smith/workflow/transition.rb', line 6

def fanout_config
  @fanout_config
end

#fromObject (readonly)

Returns the value of attribute from.



6
7
8
# File 'lib/smith/workflow/transition.rb', line 6

def from
  @from
end

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/smith/workflow/transition.rb', line 6

def name
  @name
end

#optimization_configObject (readonly)

Returns the value of attribute optimization_config.



6
7
8
# File 'lib/smith/workflow/transition.rb', line 6

def optimization_config
  @optimization_config
end

#orchestrator_configObject (readonly)

Returns the value of attribute orchestrator_config.



6
7
8
# File 'lib/smith/workflow/transition.rb', line 6

def orchestrator_config
  @orchestrator_config
end

#retry_configObject (readonly)

Returns the value of attribute retry_config.



6
7
8
# File 'lib/smith/workflow/transition.rb', line 6

def retry_config
  @retry_config
end

#router_configObject (readonly)

Returns the value of attribute router_config.



6
7
8
# File 'lib/smith/workflow/transition.rb', line 6

def router_config
  @router_config
end

#success_transitionObject (readonly)

Returns the value of attribute success_transition.



6
7
8
# File 'lib/smith/workflow/transition.rb', line 6

def success_transition
  @success_transition
end

#toObject (readonly)

Returns the value of attribute to.



6
7
8
# File 'lib/smith/workflow/transition.rb', line 6

def to
  @to
end

#workflow_classObject (readonly)

Returns the value of attribute workflow_class.



6
7
8
# File 'lib/smith/workflow/transition.rb', line 6

def workflow_class
  @workflow_class
end

Instance Method Details

#deterministic?Boolean

Returns:

  • (Boolean)


122
123
124
# File 'lib/smith/workflow/transition.rb', line 122

def deterministic?
  !@deterministic_block.nil?
end

#execute(agent_name, **opts) ⇒ Object



18
19
20
21
22
23
# File 'lib/smith/workflow/transition.rb', line 18

def execute(agent_name, **opts)
  validate_execute_conflicts!

  @agent_name = agent_name
  @agent_opts = opts
end

#fan_out(branches:) ⇒ Object Also known as: fanout



92
93
94
95
96
# File 'lib/smith/workflow/transition.rb', line 92

def fan_out(branches:)
  validate_fanout_conflicts!

  @fanout_config = { branches: normalize_fanout_branches!(branches) }
end

#fanout?Boolean

Returns:

  • (Boolean)


130
131
132
# File 'lib/smith/workflow/transition.rb', line 130

def fanout?
  !@fanout_config.nil?
end

#nested?Boolean

Returns:

  • (Boolean)


138
139
140
# File 'lib/smith/workflow/transition.rb', line 138

def nested?
  !@workflow_class.nil?
end

#on_failure(transition_name) ⇒ Object



29
30
31
# File 'lib/smith/workflow/transition.rb', line 29

def on_failure(transition_name)
  @failure_transition = transition_name
end

#on_success(transition_name) ⇒ Object



25
26
27
# File 'lib/smith/workflow/transition.rb', line 25

def on_success(transition_name)
  @success_transition = transition_name
end

#optimize(generator:, evaluator:, max_rounds:, evaluator_schema:, improvement_threshold: nil, evaluator_context: nil, before_eval: nil, on_exhaustion: :raise, on_converged: :raise, on_threshold: :raise) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/smith/workflow/transition.rb', line 61

def optimize(generator:, evaluator:, max_rounds:, evaluator_schema:,
             improvement_threshold: nil,
             evaluator_context: nil,
             before_eval: nil,
             on_exhaustion: :raise,
             on_converged: :raise,
             on_threshold: :raise)
  validate_optimize_conflicts!
  validate_optimize_controls!(generator, evaluator, max_rounds, evaluator_schema)
  validate_optimize_exit_modes!(on_exhaustion: on_exhaustion, on_converged: on_converged,
                                on_threshold: on_threshold)
  validate_optimize_evaluator_context!(evaluator_context)
  validate_optimize_before_eval!(before_eval)

  @optimization_config = {
    generator: generator, evaluator: evaluator, max_rounds: max_rounds,
    evaluator_schema: evaluator_schema, improvement_threshold: improvement_threshold,
    evaluator_context: evaluator_context,
    before_eval: before_eval,
    on_exhaustion: on_exhaustion,
    on_converged: on_converged,
    on_threshold: on_threshold
  }
end

#optimized?Boolean

Returns:

  • (Boolean)


134
135
136
# File 'lib/smith/workflow/transition.rb', line 134

def optimized?
  !@optimization_config.nil?
end

#orchestrate(**opts) ⇒ Object



86
87
88
89
90
# File 'lib/smith/workflow/transition.rb', line 86

def orchestrate(**opts)
  validate_orchestrate_conflicts!
  validate_orchestrate_controls!(opts)
  @orchestrator_config = opts
end

#orchestrated?Boolean

Returns:

  • (Boolean)


126
127
128
# File 'lib/smith/workflow/transition.rb', line 126

def orchestrated?
  !@orchestrator_config.nil?
end

#parallel?Boolean

Returns:

  • (Boolean)


146
147
148
# File 'lib/smith/workflow/transition.rb', line 146

def parallel?
  agent_opts&.dig(:parallel) == true
end

#retry_on(*error_classes, attempts:, backoff: 0, max_delay: nil, jitter: 0) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
# File 'lib/smith/workflow/transition.rb', line 99

def retry_on(*error_classes, attempts:, backoff: 0, max_delay: nil, jitter: 0)
  validate_retry_controls!(error_classes, attempts:, backoff:, max_delay:, jitter:)

  @retry_config = {
    error_classes: error_classes.freeze,
    attempts: attempts,
    backoff: Float(backoff),
    max_delay: max_delay.nil? ? nil : Float(max_delay),
    jitter: Float(jitter)
  }.freeze
end

#route(agent_name, routes:, confidence_threshold:, fallback:) ⇒ Object



33
34
35
36
37
38
39
40
41
42
# File 'lib/smith/workflow/transition.rb', line 33

def route(agent_name, routes:, confidence_threshold:, fallback:)
  validate_route_conflicts!

  @agent_name = normalize_agent_name!(agent_name, "router")
  @router_config = {
    routes: normalize_router_routes!(routes),
    confidence_threshold: normalize_confidence_threshold!(confidence_threshold),
    fallback: normalize_transition_reference!(fallback, "router fallback")
  }
end

#routed?Boolean

Returns:

  • (Boolean)


142
143
144
# File 'lib/smith/workflow/transition.rb', line 142

def routed?
  !@router_config.nil?
end

#workflow(klass) ⇒ Object

Raises:



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/smith/workflow/transition.rb', line 44

def workflow(klass)
  raise WorkflowError, "workflow binding must be a Class" unless klass.is_a?(Class)
  raise WorkflowError, "workflow binding must be a Smith::Workflow subclass" unless klass < Workflow

  validate_conflicts!(
    "workflow",
    [
      ["execute", @agent_name && !@router_config],
      ["route", @router_config],
      ["compute/run", @deterministic_block],
      ["fan_out", @fanout_config]
    ]
  )

  @workflow_class = klass
end