Class: ActiveAgent::Base Abstract
- Inherits:
-
AbstractController::Base
- Object
- AbstractController::Base
- ActiveAgent::Base
- Includes:
- AbstractController::AssetPaths, AbstractController::Caching, AbstractController::Callbacks, AbstractController::Helpers, AbstractController::Logger, AbstractController::Rendering, AbstractController::Translation, Callbacks, Delegation, Observers, Parameterized, Previews, Provider, Queueing, Rescue, Streaming, Tooling, View
- Defined in:
- lib/active_agent/base.rb
Overview
Provides AI-powered agents with prompt generation, tool calling, and conversation management.
Constant Summary collapse
- PROTECTED_OPTIONS =
%i[exception_handler stream_broadcaster tools_function]
- PROTECTED_IVARS =
AbstractController::Rendering::DEFAULT_PROTECTED_INSTANCE_VARIABLES + [ :@_action_has_layout ]
Constants included from Provider
Provider::PROVIDER_SERVICE_NAMES_REMAPS
Instance Attribute Summary collapse
-
#agent_name ⇒ String
Agent name used as a path for view lookup.
-
#embed_options ⇒ Hash
Action-level embed options merged with agent embed options.
-
#prompt_options ⇒ Hash
Action-level prompt options merged with agent prompt options.
Class Method Summary collapse
-
.default(value = nil) ⇒ Hash
(also: default_params=)
Sets default parameters applied to all actions unless overridden.
-
.embed_with(provider_reference, **agent_options) ⇒ void
Configures embedding provider and options for embedding generation.
-
.generate_with(provider_reference, **agent_options) ⇒ void
Configures generation provider and options for prompt generation.
Instance Method Summary collapse
- #action_methods ⇒ Object private
- #controller_path ⇒ Object
-
#embed(input = nil, **options) ⇒ void
Merges action-level parameters into embedding context.
-
#initialize ⇒ Base
constructor
private
A new instance of Base.
-
#logger ⇒ Logger?
Logger instance for agent operations.
-
#preview_prompt ⇒ String, Hash
Generates a preview of the prompt without executing generation.
-
#process(method_name, *args, **kwargs) ⇒ void
private
Processes an agent action with ActiveSupport::Notifications instrumentation.
-
#process_embed ⇒ ActiveAgent::Providers::Response
Executes embedding generation using configured provider and options.
-
#process_prompt ⇒ ActiveAgent::Providers::Response
(also: #process_prompt!)
Executes prompt generation using configured provider and options.
-
#prompt(*messages, **options) ⇒ void
Merges action-level parameters into prompt context.
Methods included from View
#_prefixes, #embed_view_input, #prompt_view_instructions, #prompt_view_message, #prompt_view_schema
Methods included from Tooling
Methods included from Rescue
Methods included from Delegation
#delegation_ledger, #delegation_ledger_for, #delegation_ledgers, #perform_delegation
Constructor Details
#initialize ⇒ Base
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a new instance of Base.
172 173 174 175 176 |
# File 'lib/active_agent/base.rb', line 172 def initialize # :nodoc: super self. = (self.class.&.deep_dup || {}).except(:trace_id) self. = (self.class.&.deep_dup || {}).except(:trace_id) end |
Instance Attribute Details
#agent_name ⇒ String
Agent name used as a path for view lookup.
181 182 183 |
# File 'lib/active_agent/base.rb', line 181 def agent_name @agent_name ||= self.class.anonymous? ? "anonymous" : self.class.name.underscore end |
#embed_options ⇒ Hash
Action-level embed options merged with agent embed options.
169 |
# File 'lib/active_agent/base.rb', line 169 attr_internal :embed_options |
#prompt_options ⇒ Hash
Action-level prompt options merged with agent prompt options.
164 |
# File 'lib/active_agent/base.rb', line 164 attr_internal :prompt_options |
Class Method Details
.default(value = nil) ⇒ Hash Also known as: default_params=
Sets default parameters applied to all actions unless overridden.
87 88 89 90 |
# File 'lib/active_agent/base.rb', line 87 def default(value = nil) self.default_params = default_params.merge(value).freeze if value default_params end |
.embed_with(provider_reference, **agent_options) ⇒ void
This method returns an undefined value.
Configures embedding provider and options for embedding generation.
128 129 130 131 132 133 134 135 |
# File 'lib/active_agent/base.rb', line 128 def self.(provider_reference, **) self. = provider_reference = provider_config_load(provider_reference) = self. || {} self. = .merge().merge() end |
.generate_with(provider_reference, **agent_options) ⇒ void
This method returns an undefined value.
Configures generation provider and options for prompt generation.
Options are merged with global provider config and inherited parent class options. Instructions are never inherited from parent classes.
105 106 107 108 109 110 111 112 113 114 115 116 117 118 |
# File 'lib/active_agent/base.rb', line 105 def self.generate_with(provider_reference, **) self.prompt_provider = provider_reference = provider_config_load(provider_reference) = (self. || {}).except(:instructions) # Don't inherit instructions from parent # Different Service, different APIs — discard all inherited options # to prevent parent provider config (host, api_key, etc.) leaking through if [:service] != [:service] = {} end self. = .merge().merge() end |
Instance Method Details
#action_methods ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
296 297 298 |
# File 'lib/active_agent/base.rb', line 296 def action_methods super - ActiveAgent::Base.public_instance_methods(false).map(&:to_s) - [ action_name ] end |
#controller_path ⇒ Object
159 |
# File 'lib/active_agent/base.rb', line 159 alias_method :controller_path, :agent_name |
#embed(input = nil, **options) ⇒ void
This method returns an undefined value.
Merges action-level parameters into embedding context.
236 237 238 239 |
# File 'lib/active_agent/base.rb', line 236 def (input = nil, **) = { input: }.compact_blank.merge!() .merge!() end |
#logger ⇒ Logger?
Logger instance for agent operations.
Defaults to Rails.logger when used in Rails applications. Must conform to Log4r or Ruby Logger interface.
67 |
# File 'lib/active_agent/base.rb', line 67 cattr_accessor :logger |
#preview_prompt ⇒ String, Hash
Generates a preview of the prompt without executing generation.
Useful for debugging and inspecting the final prompt that would be sent to the provider, including rendered templates and merged parameters.
269 270 271 272 273 274 275 |
# File 'lib/active_agent/base.rb', line 269 def preview_prompt fail "Prompt Provider not Configured" unless prompt_provider_klass parameters = prepare_prompt_parameters prompt_provider_klass.new(**parameters).preview end |
#process(method_name, *args, **kwargs) ⇒ void
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
This method returns an undefined value.
Processes an agent action with ActiveSupport::Notifications instrumentation.
Actions are triggered externally via Agent.action_name.generate_now or internally through tool calls during AI generation workflows.
195 196 197 198 199 200 201 |
# File 'lib/active_agent/base.rb', line 195 def process(method_name, *args, **kwargs) payload = { agent: self.class.name, action: method_name, args:, kwargs: } ActiveSupport::Notifications.instrument("process.active_agent", payload) do super end end |
#process_embed ⇒ ActiveAgent::Providers::Response
Executes embedding generation using configured provider and options.
Templates are rendered as late as possible to allow local overrides.
283 284 285 286 287 288 289 290 291 292 293 |
# File 'lib/active_agent/base.rb', line 283 def fail "Embed Provider not Configured" unless run_callbacks(:generation) do run_callbacks(:embedding) do parameters = .new(**parameters). end end end |
#process_prompt ⇒ ActiveAgent::Providers::Response Also known as: process_prompt!
Executes prompt generation using configured provider and options.
Triggered by generate_now or generate_later workflows. Templates are rendered as late as possible to allow local overrides.
248 249 250 251 252 253 254 255 256 257 258 |
# File 'lib/active_agent/base.rb', line 248 def process_prompt fail "Prompt Provider not Configured" unless prompt_provider_klass run_callbacks(:generation) do run_callbacks(:prompting) do parameters = prepare_prompt_parameters prompt_provider_klass.new(**parameters).prompt end end end |
#prompt(*messages, **options) ⇒ void
This method returns an undefined value.
Merges action-level parameters into prompt context.
Processing is deferred until execution to allow local overrides.
215 216 217 218 219 220 221 222 223 |
# File 'lib/active_agent/base.rb', line 215 def prompt(*, **) # Extract message/messages from options and add to messages array += .extract!(:message, :messages).values.flatten.compact # Extract image and document attachments += .extract!(:image, :document).map { |k, v| { k => v } } .merge!({ messages: }.compact_blank.merge!()) end |