Class: Soka::Engines::ReasoningContext
- Inherits:
-
Object
- Object
- Soka::Engines::ReasoningContext
- 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
-
#event_handler ⇒ Object
readonly
Returns the value of attribute event_handler.
-
#iteration ⇒ Object
Returns the value of attribute iteration.
-
#max_iterations ⇒ Object
readonly
Returns the value of attribute max_iterations.
-
#messages ⇒ Object
Returns the value of attribute messages.
-
#parsed_response ⇒ Object
Returns the value of attribute parsed_response.
-
#task ⇒ Object
Returns the value of attribute task.
-
#think_in ⇒ Object
Returns the value of attribute think_in.
-
#thoughts ⇒ Object
Returns the value of attribute thoughts.
Instance Method Summary collapse
-
#add_message(role:, content:) ⇒ Object
Add a message to the conversation.
-
#add_thought(thought, action: nil, observation: nil) ⇒ Object
Add a thought to the thoughts collection.
-
#current_step ⇒ Integer
Get the current iteration number (1-based for display).
-
#emit_event(type, content) ⇒ Object
Emit an event to the event handler if present.
-
#initialize(task:, event_handler: nil, max_iterations: 10, think_in: nil) ⇒ ReasoningContext
constructor
Initialize a new reasoning context.
-
#last_assistant_content ⇒ String?
Get the last assistant message content.
-
#update_last_thought(action:, observation:) ⇒ Object
Update the last thought with action and observation.
Constructor Details
#initialize(task:, event_handler: nil, max_iterations: 10, think_in: nil) ⇒ ReasoningContext
Initialize a new reasoning context
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 = [] @thoughts = [] @iteration = 0 @parsed_response = nil end |
Instance Attribute Details
#event_handler ⇒ Object (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 |
#iteration ⇒ Object
Returns the value of attribute iteration.
11 12 13 |
# File 'lib/soka/engines/reasoning_context.rb', line 11 def iteration @iteration end |
#max_iterations ⇒ Object (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 |
#messages ⇒ Object
Returns the value of attribute messages.
11 12 13 |
# File 'lib/soka/engines/reasoning_context.rb', line 11 def end |
#parsed_response ⇒ Object
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 |
#task ⇒ Object
Returns the value of attribute task.
11 12 13 |
# File 'lib/soka/engines/reasoning_context.rb', line 11 def task @task end |
#think_in ⇒ Object
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 |
#thoughts ⇒ Object
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
70 71 72 |
# File 'lib/soka/engines/reasoning_context.rb', line 70 def (role:, content:) << { role: role, content: content } end |
#add_thought(thought, action: nil, observation: nil) ⇒ Object
Add a thought to the thoughts collection
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_step ⇒ Integer
Get the current iteration number (1-based 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
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_content ⇒ String?
Get the last assistant message content
76 77 78 79 |
# File 'lib/soka/engines/reasoning_context.rb', line 76 def last_assistant_content last_assistant = .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
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 |