Class: Rubino::Interaction::State

Inherits:
Object
  • Object
show all
Defined in:
lib/rubino/interaction/state.rb

Overview

Tracks the current state of an interaction. Implements a simple state machine with valid transitions.

Constant Summary collapse

VALID_STATES =
%i[
  idle
  receiving_input
  loading_session
  loading_memory
  building_context
  checking_budget
  compressing_context
  calling_model
  persisting_session
  enqueueing_jobs
  finished
  failed
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeState

Returns a new instance of State.



25
26
27
# File 'lib/rubino/interaction/state.rb', line 25

def initialize
  @current = :idle
end

Instance Attribute Details

#currentObject (readonly)

Returns the value of attribute current.



23
24
25
# File 'lib/rubino/interaction/state.rb', line 23

def current
  @current
end

Instance Method Details

#failed?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/rubino/interaction/state.rb', line 47

def failed?
  @current == :failed
end

#finished?Boolean

Returns:

  • (Boolean)


43
44
45
# File 'lib/rubino/interaction/state.rb', line 43

def finished?
  @current == :finished
end

#idle?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/rubino/interaction/state.rb', line 39

def idle?
  @current == :idle
end

#terminal?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/rubino/interaction/state.rb', line 51

def terminal?
  finished? || failed?
end

#transition_to!(new_state, event_bus: nil) ⇒ Object

Transitions to a new state, emitting an event

Raises:



30
31
32
33
34
35
36
37
# File 'lib/rubino/interaction/state.rb', line 30

def transition_to!(new_state, event_bus: nil)
  raise Error, "Invalid state: #{new_state}" unless VALID_STATES.include?(new_state)

  old_state = @current
  @current = new_state

  event_bus&.emit(Events::STATUS_CHANGED, from: old_state, to: new_state)
end