Class: Smith::Workflow::DeterministicStep

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context:, session_messages:, tool_results:, state:, transition_name:) ⇒ DeterministicStep

Returns a new instance of DeterministicStep.



9
10
11
12
13
14
15
16
17
18
# File 'lib/smith/workflow/deterministic_step.rb', line 9

def initialize(context:, session_messages:, tool_results:, state:, transition_name:)
  @context = context
  @session_messages = session_messages
  @tool_results = tool_results
  @current_state = state
  @transition_name = transition_name
  @context_writes = {}
  @routed_to = nil
  @outcome = nil
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



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

def context
  @context
end

#context_writesObject (readonly)

Returns the value of attribute context_writes.



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

def context_writes
  @context_writes
end

#current_stateObject (readonly)

Returns the value of attribute current_state.



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

def current_state
  @current_state
end

#outcomeObject (readonly)

Returns the value of attribute outcome.



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

def outcome
  @outcome
end

#routed_toObject (readonly)

Returns the value of attribute routed_to.



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

def routed_to
  @routed_to
end

#session_messagesObject (readonly)

Returns the value of attribute session_messages.



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

def session_messages
  @session_messages
end

#tool_resultsObject (readonly)

Returns the value of attribute tool_results.



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

def tool_results
  @tool_results
end

#transition_nameObject (readonly)

Returns the value of attribute transition_name.



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

def transition_name
  @transition_name
end

Instance Method Details

#fail!(message, retryable: nil, kind: nil, details: nil) ⇒ Object



52
53
54
# File 'lib/smith/workflow/deterministic_step.rb', line 52

def fail!(message, retryable: nil, kind: nil, details: nil)
  raise DeterministicStepFailure.new(message, retryable: retryable, kind: kind, details: details)
end

#last_outputObject Also known as: output



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

def last_output
  return @last_output if defined?(@last_output)

  msg = session_messages.reverse.find { |m| m[:role] == :assistant || m[:role] == "assistant" }
  @last_output = msg&.dig(:content)
end

#read_context(key) ⇒ Object



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

def read_context(key)
  @context_writes.key?(key) ? @context_writes[key] : context[key]
end

#route_to(transition_name) ⇒ Object

Raises:



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

def route_to(transition_name)
  raise WorkflowError, "route_to already called with :#{@routed_to}" if @routed_to

  @routed_to = transition_name
end

#write_context(key, value) ⇒ Object

Raises:



33
34
35
36
37
# File 'lib/smith/workflow/deterministic_step.rb', line 33

def write_context(key, value)
  raise WorkflowError, "write_context key must be a Symbol, got #{key.class}" unless key.is_a?(Symbol)

  @context_writes[key] = value
end

#write_outcome(kind:, payload:) ⇒ Object

Raises:



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

def write_outcome(kind:, payload:)
  raise WorkflowError, "write_outcome kind must be a Symbol, got #{kind.class}" unless kind.is_a?(Symbol)
  raise WorkflowError, "write_outcome already called with :#{@outcome[:kind]}" if @outcome

  @outcome = { kind: kind, payload: payload }
end