Class: RubyPi::Agent::Core
- Inherits:
-
Object
- Object
- RubyPi::Agent::Core
- Includes:
- EventEmitter
- Defined in:
- lib/ruby_pi/agent/core.rb
Overview
The main agent class. Wraps State, Loop, and EventEmitter into a cohesive interface for running agentic LLM interactions with tool use, streaming, and lifecycle hooks.
Instance Attribute Summary collapse
-
#config ⇒ RubyPi::Configuration?
readonly
Per-agent configuration handle exposed for inspection only — see #config= caveats below.
-
#extensions ⇒ Array<Class>
readonly
Registered extension classes for introspection.
-
#state ⇒ RubyPi::Agent::State
readonly
The agent’s mutable state.
Instance Method Summary collapse
-
#continue(prompt) ⇒ RubyPi::Agent::Result
Continues the conversation with a follow-up user message.
-
#effective_config ⇒ RubyPi::Configuration
Returns the effective configuration for this agent.
-
#initialize(system_prompt:, model:, tools: nil, messages: [], max_iterations: 10, transform_context: nil, before_tool_call: nil, after_tool_call: nil, compaction: nil, user_data: {}, config: nil, execution_mode: :parallel, tool_timeout: 30) ⇒ Core
constructor
Creates a new Agent instance.
-
#run(prompt) ⇒ RubyPi::Agent::Result
Runs the agent with an initial user prompt.
-
#use(extension_class) ⇒ void
Registers an extension with this agent.
Methods included from EventEmitter
Constructor Details
#initialize(system_prompt:, model:, tools: nil, messages: [], max_iterations: 10, transform_context: nil, before_tool_call: nil, after_tool_call: nil, compaction: nil, user_data: {}, config: nil, execution_mode: :parallel, tool_timeout: 30) ⇒ Core
Creates a new Agent instance.
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/ruby_pi/agent/core.rb', line 89 def initialize( system_prompt:, model:, tools: nil, messages: [], max_iterations: 10, transform_context: nil, before_tool_call: nil, after_tool_call: nil, compaction: nil, user_data: {}, config: nil, execution_mode: :parallel, tool_timeout: 30 ) @state = State.new( system_prompt: system_prompt, model: model, tools: tools, messages: , max_iterations: max_iterations, transform_context: transform_context, before_tool_call: before_tool_call, after_tool_call: after_tool_call, user_data: user_data ) @compaction = compaction @extensions = [] @config = config @execution_mode = execution_mode @tool_timeout = tool_timeout end |
Instance Attribute Details
#config ⇒ RubyPi::Configuration? (readonly)
Returns per-agent configuration handle exposed for inspection only — see #config= caveats below. The actual provider config is resolved at model construction time:
custom = RubyPi::Configuration.new
custom.openai_api_key = "sk-..."
model = RubyPi::LLM.model(:openai, "gpt-4o", config: custom)
agent = RubyPi::Agent.new(model: model, config: custom, ...)
Passing ‘config:` to `Agent.new` does NOT retroactively change the model’s behavior — it is informational, intended for inspection by transforms and extensions. To override provider config you must pass ‘config:` to the model factory as shown above.
64 65 66 |
# File 'lib/ruby_pi/agent/core.rb', line 64 def config @config end |
#extensions ⇒ Array<Class> (readonly)
Returns registered extension classes for introspection.
49 50 51 |
# File 'lib/ruby_pi/agent/core.rb', line 49 def extensions @extensions end |
#state ⇒ RubyPi::Agent::State (readonly)
Returns the agent’s mutable state.
46 47 48 |
# File 'lib/ruby_pi/agent/core.rb', line 46 def state @state end |
Instance Method Details
#continue(prompt) ⇒ RubyPi::Agent::Result
Continues the conversation with a follow-up user message. Preserves the existing conversation history and appends the new prompt before resuming the loop.
Issue #16: Uses the encapsulated reset_iteration! method instead of the old approach that bypassed encapsulation and was fragile.
149 150 151 152 153 |
# File 'lib/ruby_pi/agent/core.rb', line 149 def continue(prompt) @state.reset_iteration! @state.(role: :user, content: prompt) execute_loop end |
#effective_config ⇒ RubyPi::Configuration
Returns the effective configuration for this agent. If a per-agent config was provided, returns that; otherwise falls back to the global RubyPi.configuration.
184 185 186 |
# File 'lib/ruby_pi/agent/core.rb', line 184 def effective_config @config || RubyPi.configuration end |
#run(prompt) ⇒ RubyPi::Agent::Result
Runs the agent with an initial user prompt. Adds the prompt to the conversation history, executes the think-act-observe loop, emits :agent_end when done, and returns the result.
Issue #16: Resets the iteration counter at the start of each run() call using the encapsulated reset_iteration! method. Previously, the counter was never reset on run(), so a second call to run() on the same agent instance could immediately trip max_iterations_reached?.
133 134 135 136 137 |
# File 'lib/ruby_pi/agent/core.rb', line 133 def run(prompt) @state.reset_iteration! @state.(role: :user, content: prompt) execute_loop end |
#use(extension_class) ⇒ void
This method returns an undefined value.
Registers an extension with this agent. The extension’s hooks are automatically subscribed to the agent’s events.
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
# File 'lib/ruby_pi/agent/core.rb', line 161 def use(extension_class) unless extension_class.respond_to?(:hooks) raise ArgumentError, "Expected an extension class with a .hooks method, got #{extension_class.inspect}" end # Subscribe each hook to the corresponding event extension_class.hooks.each do |event, handlers| handlers.each do |handler| on(event) do |data| handler.call(data, self) end end end @extensions << extension_class end |