Class: RcrewAI::Rails::Tools::ActionMailerTool

Inherits:
RCrewAI::Tools::Base
  • Object
show all
Defined in:
lib/rcrewai/rails/tools/action_mailer_tool.rb

Instance Method Summary collapse

Constructor Details

#initialize(mailer_class: nil, allowed_methods: []) ⇒ ActionMailerTool

Returns a new instance of ActionMailerTool.



20
21
22
23
24
# File 'lib/rcrewai/rails/tools/action_mailer_tool.rb', line 20

def initialize(mailer_class: nil, allowed_methods: [])
  super()
  @mailer_class = mailer_class
  @allowed_methods = allowed_methods.map(&:to_sym)
end

Instance Method Details

#execute(mailer_method:, mailer: nil, params: {}, deliver_method: "deliver_later", at: nil) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/rcrewai/rails/tools/action_mailer_tool.rb', line 26

def execute(mailer_method:, mailer: nil, params: {}, deliver_method: "deliver_later", at: nil)
  method_sym = mailer_method.to_sym
  validate_mailer_method!(method_sym)

  mailer_klass = resolve_mailer_class(mailer)
  mailer_params = (params || {}).transform_keys(&:to_sym)
  mail = mailer_klass.public_send(method_sym, **mailer_params)

  case deliver_method.to_sym
  when :deliver_now
    mail.deliver_now
    { status: "sent", method: method_sym, delivered_at: Time.current }
  when :deliver_later
    job = mail.deliver_later
    { status: "queued", method: method_sym, job_id: job.job_id }
  when :deliver_later_at
    delivery_time = at.present? ? Time.iso8601(at) : 1.hour.from_now
    job = mail.deliver_later(wait_until: delivery_time)
    { status: "scheduled", method: method_sym, job_id: job.job_id, scheduled_for: delivery_time }
  else
    { error: "Unknown delivery method: #{deliver_method}" }
  end
rescue => e
  { error: "Failed to send email", message: e.message }
end