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
-
#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.
-
#initialize(system_prompt:, model:, tools: nil, max_iterations: 10, transform_context: nil, before_tool_call: nil, after_tool_call: nil, compaction: nil, user_data: {}) ⇒ 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, max_iterations: 10, transform_context: nil, before_tool_call: nil, after_tool_call: nil, compaction: nil, user_data: {}) ⇒ Core
Creates a new Agent instance.
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/ruby_pi/agent/core.rb', line 59 def initialize( system_prompt:, model:, tools: nil, max_iterations: 10, transform_context: nil, before_tool_call: nil, after_tool_call: nil, compaction: nil, user_data: {} ) @state = State.new( system_prompt: system_prompt, model: model, tools: tools, 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 = [] end |
Instance Attribute Details
#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.
101 102 103 104 105 106 |
# File 'lib/ruby_pi/agent/core.rb', line 101 def continue(prompt) # Reset the iteration counter for the new run while keeping history @state.instance_variable_set(:@iteration, 0) @state.(role: :user, content: prompt) execute_loop 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.
90 91 92 93 |
# File 'lib/ruby_pi/agent/core.rb', line 90 def run(prompt) @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.
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 |
# File 'lib/ruby_pi/agent/core.rb', line 114 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 |