Class: Soka::Engines::ReasoningContext

Inherits:
Object
  • Object
show all
Defined in:
lib/soka/engines/reasoning_context.rb

Overview

Context object that encapsulates all data needed during reasoning process This eliminates the need to pass multiple parameters and blocks through method chains

Defined Under Namespace

Classes: Event

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(task:, event_handler: nil, max_iterations: 10, think_in: nil) ⇒ ReasoningContext

Initialize a new reasoning context

Parameters:

  • task (String)

    The task to be processed

  • event_handler (Proc, nil) (defaults to: nil)

    Optional block to handle events

  • max_iterations (Integer) (defaults to: 10)

    Maximum number of reasoning iterations

  • think_in (String, nil) (defaults to: nil)

    The language to use for thinking



19
20
21
22
23
24
25
26
27
28
# File 'lib/soka/engines/reasoning_context.rb', line 19

def initialize(task:, event_handler: nil, max_iterations: 10, think_in: nil)
  @task = task
  @event_handler = event_handler
  @max_iterations = max_iterations
  @think_in = think_in
  @messages = []
  @thoughts = []
  @iteration = 0
  @parsed_response = nil
end

Instance Attribute Details

#event_handlerObject (readonly)

Returns the value of attribute event_handler.



12
13
14
# File 'lib/soka/engines/reasoning_context.rb', line 12

def event_handler
  @event_handler
end

#iterationObject

Returns the value of attribute iteration.



11
12
13
# File 'lib/soka/engines/reasoning_context.rb', line 11

def iteration
  @iteration
end

#max_iterationsObject (readonly)

Returns the value of attribute max_iterations.



12
13
14
# File 'lib/soka/engines/reasoning_context.rb', line 12

def max_iterations
  @max_iterations
end

#messagesObject

Returns the value of attribute messages.



11
12
13
# File 'lib/soka/engines/reasoning_context.rb', line 11

def messages
  @messages
end

#parsed_responseObject

Returns the value of attribute parsed_response.



11
12
13
# File 'lib/soka/engines/reasoning_context.rb', line 11

def parsed_response
  @parsed_response
end

#taskObject

Returns the value of attribute task.



11
12
13
# File 'lib/soka/engines/reasoning_context.rb', line 11

def task
  @task
end

#think_inObject

Returns the value of attribute think_in.



11
12
13
# File 'lib/soka/engines/reasoning_context.rb', line 11

def think_in
  @think_in
end

#thoughtsObject

Returns the value of attribute thoughts.



11
12
13
# File 'lib/soka/engines/reasoning_context.rb', line 11

def thoughts
  @thoughts
end

Instance Method Details

#add_message(role:, content:) ⇒ Object

Add a message to the conversation

Parameters:

  • role (String)

    The role of the message sender

  • content (String)

    The message content



70
71
72
# File 'lib/soka/engines/reasoning_context.rb', line 70

def add_message(role:, content:)
  @messages << { role: role, content: content }
end

#add_thought(thought, action: nil, observation: nil) ⇒ Object

Add a thought to the thoughts collection

Parameters:

  • thought (String)

    The thought content

  • action (Hash, nil) (defaults to: nil)

    Optional action associated with the thought

  • observation (String, nil) (defaults to: nil)

    Optional observation from action



50
51
52
53
54
55
# File 'lib/soka/engines/reasoning_context.rb', line 50

def add_thought(thought, action: nil, observation: nil)
  thought_data = { step: current_step, thought: thought }
  thought_data[:action] = action if action
  thought_data[:observation] = observation if observation
  @thoughts << thought_data
end

#current_stepInteger

Get the current iteration number (1-based for display)

Returns:

  • (Integer)

    The current iteration number for display



42
43
44
# File 'lib/soka/engines/reasoning_context.rb', line 42

def current_step
  @iteration + 1
end

#emit_event(type, content) ⇒ Object

Emit an event to the event handler if present

Parameters:

  • type (Symbol)

    The type of event (e.g., :thought, :action, :observation)

  • content (String, Hash)

    The content of the event



33
34
35
36
37
38
# File 'lib/soka/engines/reasoning_context.rb', line 33

def emit_event(type, content)
  return unless @event_handler

  event = Event.new(type, content)
  @event_handler.call(event)
end

#last_assistant_contentString?

Get the last assistant message content

Returns:

  • (String, nil)

    The content of the last assistant message



76
77
78
79
# File 'lib/soka/engines/reasoning_context.rb', line 76

def last_assistant_content
  last_assistant = @messages.reverse.find { |msg| msg[:role] == 'assistant' }
  last_assistant&.fetch(:content, nil)
end

#update_last_thought(action:, observation:) ⇒ Object

Update the last thought with action and observation

Parameters:

  • action (Hash)

    The action that was taken

  • observation (String)

    The observation from the action



60
61
62
63
64
65
# File 'lib/soka/engines/reasoning_context.rb', line 60

def update_last_thought(action:, observation:)
  return if @thoughts.empty?

  @thoughts.last[:action] = action
  @thoughts.last[:observation] = observation
end