Class: RubyPi::Agent::State
- Inherits:
-
Object
- Object
- RubyPi::Agent::State
- Defined in:
- lib/ruby_pi/agent/state.rb
Overview
Mutable state object threaded through the agent loop. Encapsulates the full conversation history, configuration, and hook callables so that the loop, compaction, and transforms all operate on a single shared object.
Instance Attribute Summary collapse
-
#after_tool_call ⇒ Proc?
Callable invoked after each tool call; receives the ToolCall and the RubyPi::Tools::Result.
-
#before_tool_call ⇒ Proc?
Callable invoked before each tool call; receives the RubyPi::LLM::ToolCall.
-
#max_iterations ⇒ Integer
readonly
Maximum think-act-observe iterations before halting.
-
#model ⇒ RubyPi::LLM::BaseProvider
readonly
The LLM provider instance.
-
#system_prompt ⇒ String
The system prompt prepended to every LLM call.
-
#tools ⇒ RubyPi::Tools::Registry
readonly
The registry of available tools.
-
#transform_context ⇒ Proc?
Callable invoked with state before each LLM call to transform context (system prompt, messages).
-
#user_data ⇒ Hash
Arbitrary user-provided data accessible by transforms and extensions.
Instance Method Summary collapse
-
#add_message(role:, content: nil, **options) ⇒ Array<Hash>
Appends a message to the conversation history.
-
#increment_iteration! ⇒ Integer
Increments the iteration counter by one.
-
#initialize(system_prompt:, model:, tools: nil, messages: [], max_iterations: 10, transform_context: nil, before_tool_call: nil, after_tool_call: nil, user_data: {}) ⇒ State
constructor
Creates a new State instance with the given configuration.
-
#inspect ⇒ String
Provides a human-readable summary of the current state for debugging.
-
#iteration ⇒ Integer
Returns the current iteration count (number of completed think-act-observe cycles).
-
#max_iterations_reached? ⇒ Boolean
Returns true if the iteration count has reached or exceeded max_iterations.
-
#messages ⇒ Array<Hash>
Returns a frozen copy of the conversation history.
-
#messages=(new_messages) ⇒ Array<Hash>
Replaces the entire conversation history.
Constructor Details
#initialize(system_prompt:, model:, tools: nil, messages: [], max_iterations: 10, transform_context: nil, before_tool_call: nil, after_tool_call: nil, user_data: {}) ⇒ State
Creates a new State instance with the given configuration.
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/ruby_pi/agent/state.rb', line 69 def initialize( system_prompt:, model:, tools: nil, messages: [], max_iterations: 10, transform_context: nil, before_tool_call: nil, after_tool_call: nil, user_data: {} ) @system_prompt = system_prompt @model = model @tools = tools @messages = Array().dup @max_iterations = max_iterations @transform_context = transform_context @before_tool_call = before_tool_call @after_tool_call = after_tool_call @user_data = user_data @iteration = 0 end |
Instance Attribute Details
#after_tool_call ⇒ Proc?
Returns callable invoked after each tool call; receives the ToolCall and the RubyPi::Tools::Result.
52 53 54 |
# File 'lib/ruby_pi/agent/state.rb', line 52 def after_tool_call @after_tool_call end |
#before_tool_call ⇒ Proc?
Returns callable invoked before each tool call; receives the RubyPi::LLM::ToolCall.
48 49 50 |
# File 'lib/ruby_pi/agent/state.rb', line 48 def before_tool_call @before_tool_call end |
#max_iterations ⇒ Integer (readonly)
Returns maximum think-act-observe iterations before halting.
40 41 42 |
# File 'lib/ruby_pi/agent/state.rb', line 40 def max_iterations @max_iterations end |
#model ⇒ RubyPi::LLM::BaseProvider (readonly)
Returns the LLM provider instance.
34 35 36 |
# File 'lib/ruby_pi/agent/state.rb', line 34 def model @model end |
#system_prompt ⇒ String
Returns the system prompt prepended to every LLM call.
31 32 33 |
# File 'lib/ruby_pi/agent/state.rb', line 31 def system_prompt @system_prompt end |
#tools ⇒ RubyPi::Tools::Registry (readonly)
Returns the registry of available tools.
37 38 39 |
# File 'lib/ruby_pi/agent/state.rb', line 37 def tools @tools end |
#transform_context ⇒ Proc?
Returns callable invoked with state before each LLM call to transform context (system prompt, messages).
44 45 46 |
# File 'lib/ruby_pi/agent/state.rb', line 44 def transform_context @transform_context end |
#user_data ⇒ Hash
Returns arbitrary user-provided data accessible by transforms and extensions.
56 57 58 |
# File 'lib/ruby_pi/agent/state.rb', line 56 def user_data @user_data end |
Instance Method Details
#add_message(role:, content: nil, **options) ⇒ Array<Hash>
Appends a message to the conversation history.
98 99 100 101 102 |
# File 'lib/ruby_pi/agent/state.rb', line 98 def (role:, content: nil, **) = { role: role.to_sym, content: content }.merge() @messages << @messages end |
#increment_iteration! ⇒ Integer
Increments the iteration counter by one. Called by the agent loop at the end of each think-act-observe cycle.
133 134 135 |
# File 'lib/ruby_pi/agent/state.rb', line 133 def increment_iteration! @iteration += 1 end |
#inspect ⇒ String
Provides a human-readable summary of the current state for debugging.
147 148 149 150 151 152 |
# File 'lib/ruby_pi/agent/state.rb', line 147 def inspect "#<RubyPi::Agent::State " \ "iteration=#{@iteration}/#{@max_iterations} " \ "messages=#{@messages.size} " \ "tools=#{@tools&.size || 0}>" end |
#iteration ⇒ Integer
Returns the current iteration count (number of completed think-act-observe cycles).
125 126 127 |
# File 'lib/ruby_pi/agent/state.rb', line 125 def iteration @iteration end |
#max_iterations_reached? ⇒ Boolean
Returns true if the iteration count has reached or exceeded max_iterations.
140 141 142 |
# File 'lib/ruby_pi/agent/state.rb', line 140 def max_iterations_reached? @iteration >= @max_iterations end |
#messages ⇒ Array<Hash>
Returns a frozen copy of the conversation history. Callers cannot accidentally mutate the internal array through this reference.
108 109 110 |
# File 'lib/ruby_pi/agent/state.rb', line 108 def @messages.dup.freeze end |
#messages=(new_messages) ⇒ Array<Hash>
Replaces the entire conversation history. Used by compaction to swap in a shortened message array.
117 118 119 |
# File 'lib/ruby_pi/agent/state.rb', line 117 def () @messages = Array().dup end |