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, 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
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.
169 170 171 172 173 |
# File 'lib/active_agent/base.rb', line 169 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.
178 179 180 |
# File 'lib/active_agent/base.rb', line 178 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.
166 |
# File 'lib/active_agent/base.rb', line 166 attr_internal :embed_options |
#prompt_options ⇒ Hash
Action-level prompt options merged with agent prompt options.
161 |
# File 'lib/active_agent/base.rb', line 161 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.
85 86 87 88 |
# File 'lib/active_agent/base.rb', line 85 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.
125 126 127 128 129 130 131 132 |
# File 'lib/active_agent/base.rb', line 125 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.
103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/active_agent/base.rb', line 103 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 if [:service] != [:service] .extract!(:service, :api_version) 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.
293 294 295 |
# File 'lib/active_agent/base.rb', line 293 def action_methods super - ActiveAgent::Base.public_instance_methods(false).map(&:to_s) - [ action_name ] end |
#controller_path ⇒ Object
156 |
# File 'lib/active_agent/base.rb', line 156 alias_method :controller_path, :agent_name |
#embed(input = nil, **options) ⇒ void
This method returns an undefined value.
Merges action-level parameters into embedding context.
233 234 235 236 |
# File 'lib/active_agent/base.rb', line 233 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.
65 |
# File 'lib/active_agent/base.rb', line 65 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.
266 267 268 269 270 271 272 |
# File 'lib/active_agent/base.rb', line 266 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.
192 193 194 195 196 197 198 |
# File 'lib/active_agent/base.rb', line 192 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.
280 281 282 283 284 285 286 287 288 289 290 |
# File 'lib/active_agent/base.rb', line 280 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.
245 246 247 248 249 250 251 252 253 254 255 |
# File 'lib/active_agent/base.rb', line 245 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.
212 213 214 215 216 217 218 219 220 |
# File 'lib/active_agent/base.rb', line 212 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 |