Class: ActiveAgent::Base

Inherits:
AbstractController::Base
  • Object
show all
Includes:
AbstractController::AssetPaths, AbstractController::Caching, AbstractController::Callbacks, AbstractController::Helpers, AbstractController::Logger, AbstractController::Rendering, AbstractController::Translation, ActionView::Layouts, Callbacks, GenerationMethods, GenerationProvider, Parameterized, Previews, QueuedGeneration, Rescuable
Defined in:
lib/active_agent/base.rb

Defined Under Namespace

Classes: LateAttachmentsProxy, NullPrompt

Constant Summary collapse

PROTECTED_IVARS =
AbstractController::Rendering::DEFAULT_PROTECTED_INSTANCE_VARIABLES + [:@_action_has_layout]

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Rescuable

#handle_exceptions

Methods included from GenerationProvider

#generation_provider

Methods included from GenerationMethods

#wrap_generation_behavior!

Constructor Details

#initializeBase

Returns a new instance of Base.



213
214
215
216
217
# File 'lib/active_agent/base.rb', line 213

def initialize
  super
  @_prompt_was_called = false
  @_context = ActiveAgent::ActionPrompt::Prompt.new(instructions: options[:instructions])
end

Class Attribute Details

.agent_nameObject

Returns the name of the current agent. This method is also being used as a path for a view lookup. If this is an anonymous agent, this method will return anonymous instead.



144
145
146
# File 'lib/active_agent/base.rb', line 144

def agent_name
  @agent_name ||= anonymous? ? "anonymous" : name.underscore
end

Class Method Details

.controller_pathObject

Returns the name of the current agent. This method is also being used as a path for a view lookup. If this is an anonymous agent, this method will return anonymous instead.



149
150
151
# File 'lib/active_agent/base.rb', line 149

def agent_name
  @agent_name ||= anonymous? ? "anonymous" : name.underscore
end

.default(value = nil) ⇒ Object Also known as: default_options=

Sets the defaults through app configuration:

config.action_agent.default(from: "[email protected]")

Aliased by ::default_options=



156
157
158
159
# File 'lib/active_agent/base.rb', line 156

def default(value = nil)
  self.default_params = default_params.merge(value).freeze if value
  default_params
end

.generate_prompt(prompt) ⇒ Object

Wraps a prompt generation inside of ActiveSupport::Notifications instrumentation.

This method is actually called by the ActionPrompt::Prompt object itself through a callback when you call :generate_prompt on the ActionPrompt::Prompt, calling generate_prompt directly and passing an ActionPrompt::Prompt will do nothing except tell the logger you generated the prompt.



171
172
173
174
175
176
# File 'lib/active_agent/base.rb', line 171

def generate_prompt(prompt) # :nodoc:
  ActiveSupport::Notifications.instrument("deliver.active_agent") do |payload|
    set_payload_for_prompt(payload, prompt)
    yield # Let Prompt do the generation actions
  end
end

.generate_with(provider, **options) ⇒ Object

Define how the agent should generate content



133
134
135
136
# File 'lib/active_agent/base.rb', line 133

def generate_with(provider, **options)
  self.generation_provider = provider
  self.options = (options || {}).merge(options)
end

.observer_class_for(value) ⇒ Object

:nodoc:



122
123
124
125
126
127
128
129
# File 'lib/active_agent/base.rb', line 122

def observer_class_for(value) # :nodoc:
  case value
  when String, Symbol
    value.to_s.camelize.constantize
  else
    value
  end
end

.promptObject



70
71
72
# File 'lib/active_agent/base.rb', line 70

def prompt(...)
  new.prompt(...)
end

.register_interceptor(interceptor) ⇒ Object

Register an Interceptor which will be called before mail is sent. Either a class, string, or symbol can be passed in as the Interceptor. If a string or symbol is passed in it will be camelized and constantized.



111
112
113
# File 'lib/active_agent/base.rb', line 111

def register_interceptor(interceptor)
  Mail.register_interceptor(observer_class_for(interceptor))
end

.register_interceptors(*interceptors) ⇒ Object

Register one or more Interceptors which will be called before mail is sent.



85
86
87
# File 'lib/active_agent/base.rb', line 85

def register_interceptors(*interceptors)
  interceptors.flatten.compact.each { |interceptor| register_interceptor(interceptor) }
end

.register_observer(observer) ⇒ Object

Register an Observer which will be notified when mail is delivered. Either a class, string, or symbol can be passed in as the Observer. If a string or symbol is passed in it will be camelized and constantized.



97
98
99
# File 'lib/active_agent/base.rb', line 97

def register_observer(observer)
  Mail.register_observer(observer_class_for(observer))
end

.register_observers(*observers) ⇒ Object

Register one or more Observers which will be notified when mail is delivered.



75
76
77
# File 'lib/active_agent/base.rb', line 75

def register_observers(*observers)
  observers.flatten.compact.each { |observer| register_observer(observer) }
end

.stream_with(&stream) ⇒ Object



138
139
140
# File 'lib/active_agent/base.rb', line 138

def stream_with(&stream)
  self.options = (options || {}).merge(stream: stream)
end

.unregister_interceptor(interceptor) ⇒ Object

Unregister a previously registered Interceptor. Either a class, string, or symbol can be passed in as the Interceptor. If a string or symbol is passed in it will be camelized and constantized.



118
119
120
# File 'lib/active_agent/base.rb', line 118

def unregister_interceptor(interceptor)
  Mail.unregister_interceptor(observer_class_for(interceptor))
end

.unregister_interceptors(*interceptors) ⇒ Object

Unregister one or more previously registered Interceptors.



90
91
92
# File 'lib/active_agent/base.rb', line 90

def unregister_interceptors(*interceptors)
  interceptors.flatten.compact.each { |interceptor| unregister_interceptor(interceptor) }
end

.unregister_observer(observer) ⇒ Object

Unregister a previously registered Observer. Either a class, string, or symbol can be passed in as the Observer. If a string or symbol is passed in it will be camelized and constantized.



104
105
106
# File 'lib/active_agent/base.rb', line 104

def unregister_observer(observer)
  Mail.unregister_observer(observer_class_for(observer))
end

.unregister_observers(*observers) ⇒ Object

Unregister one or more previously registered Observers.



80
81
82
# File 'lib/active_agent/base.rb', line 80

def unregister_observers(*observers)
  observers.flatten.compact.each { |observer| unregister_observer(observer) }
end

Instance Method Details

#action_schemasObject



310
311
312
313
314
# File 'lib/active_agent/base.rb', line 310

def action_schemas
  action_methods.map do |action|
    JSON.parse render_to_string(locals: {action_name: action}, action: action, formats: :json)
  end
end

#agent_nameObject

Returns the name of the agent object.



252
253
254
# File 'lib/active_agent/base.rb', line 252

def agent_name
  self.class.agent_name
end

#headers(args = nil) ⇒ Object



256
257
258
259
260
261
262
# File 'lib/active_agent/base.rb', line 256

def headers(args = nil)
  if args
    @_context.headers(args)
  else
    @_context
  end
end

#perform_generationObject



208
209
210
211
# File 'lib/active_agent/base.rb', line 208

def perform_generation
  context.options.merge(options)
  generation_provider.generate(context) if context && generation_provider
end

#process(method_name, *args) ⇒ Object

:nodoc:



219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/active_agent/base.rb', line 219

def process(method_name, *args) # :nodoc:
  payload = {
    agent: self.class.name,
    action: method_name,
    args: args
  }

  ActiveSupport::Notifications.instrument("process.active_agent", payload) do
    super
    @_context = ActiveAgent::ActionPrompt::Prompt.new unless @_prompt_was_called
  end
end

#prompt(headers = {}, &block) ⇒ Object



289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
# File 'lib/active_agent/base.rb', line 289

def prompt(headers = {}, &block)
  return context if @_prompt_was_called && headers.blank? && !block

  content_type = headers[:content_type]

  headers = apply_defaults(headers)

  context.charset = charset = headers[:charset]

  responses = collect_responses(headers, &block)
  @_prompt_was_called = true

  create_parts_from_responses(context, responses)

  context.content_type = set_content_type(context, content_type, headers[:content_type])
  context.charset = charset
  context.actions = headers[:actions] || action_schemas
  binding.irb
  context
end