Class: Profiler::MCP::Tools::GetProfileMailers

Inherits:
Object
  • Object
show all
Defined in:
lib/profiler/mcp/tools/get_profile_mailers.rb

Constant Summary collapse

SEVERITY_ICONS =
{ "deliver_now" => "", "deliver_later" => "📬", "queued" => "" }.freeze

Class Method Summary collapse

Class Method Details

.append_email_section(lines, title, emails, profile, params) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/profiler/mcp/tools/get_profile_mailers.rb', line 72

def self.append_email_section(lines, title, emails, profile, params)
  lines << ""
  lines << "## #{title}\n"

  emails.each_with_index do |email, i|
    lines << "### Email #{i + 1}: #{email['mailer_class']}##{email['action']}"
    lines << "- **Subject:** #{email['subject']}"
    lines << "- **To:** #{Array(email['to']).join(', ')}"
    lines << "- **From:** #{Array(email['from']).join(', ')}"
    lines << "- **CC:** #{Array(email['cc']).join(', ')}"          unless Array(email['cc']).empty?
    lines << "- **BCC:** #{Array(email['bcc']).join(', ')}"        unless Array(email['bcc']).empty?
    lines << "- **Reply-To:** #{Array(email['reply_to']).join(', ')}" unless Array(email['reply_to']).empty?
    lines << "- **Message-ID:** #{email['message_id']}"            if email['message_id']
    lines << "- **Delivery method:** #{email['delivery_method']}"
    lines << "- **Mode:** #{email['delivery_mode']}"
    lines << "- **Render duration:** #{email['duration_ms']}ms"    if email['duration_ms']
    lines << "- **Delivery duration:** #{email['delivery_ms']}ms"  if email['delivery_ms']
    lines << "- **Error:** #{email['error']}"                      if email['error']

    parts = Array(email['parts'])
    lines << "- **Parts:** #{parts.join(', ')}" if parts.any?

    attachments = Array(email['attachments'])
    if attachments.any?
      lines << "- **Attachments:**"
      attachments.each { |a| lines << "  - #{a['filename']} (#{a['size']} bytes)" }
    end

    assigns = email['assigns'] || {}
    if assigns.any?
      lines << "- **Template assigns:**"
      assigns.each { |k, v| lines << "  - `#{k}`: #{v}" }
    end

    if email['body_captured']
      if email['body_html'] && !email['body_html'].empty?
        lines << "- **HTML body:**"
        formatted = BodyFormatter.format_body(
          profile.token, "mailer_#{i}_html", email['body_html'], nil, params
        )
        lines << formatted if formatted
      end
      if email['body_text'] && !email['body_text'].empty?
        lines << "- **Text body:**"
        formatted = BodyFormatter.format_body(
          profile.token, "mailer_#{i}_text", email['body_text'], nil, params
        )
        lines << formatted if formatted
      end
    else
      lines << "- **Body:** not captured (enable `capture_mail_body: true` in initializer)"
    end

    lines << ""
  end
end

.append_queued_section(lines, queued) ⇒ Object



129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/profiler/mcp/tools/get_profile_mailers.rb', line 129

def self.append_queued_section(lines, queued)
  lines << ""
  lines << "## Queued (deliver_later)\n"
  queued.each_with_index do |email, i|
    lines << "### Queued #{i + 1}: #{email['mailer_class']}##{email['action']}"
    lines << "- **Delivery method:** #{email['delivery_method']}"
    lines << "- **Render duration:** #{email['duration_ms']}ms" if email['duration_ms']
    assigns = email['assigns'] || {}
    if assigns.any?
      lines << "- **Template assigns:**"
      assigns.each { |k, v| lines << "  - `#{k}`: #{v}" }
    end
    lines << ""
  end
end

.call(params) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/profiler/mcp/tools/get_profile_mailers.rb', line 9

def self.call(params)
  token = params["token"]
  unless token
    return [{ type: "text", text: "Error: token parameter is required" }]
  end

  profile = if token == "latest"
    Profiler.storage.list(limit: 1).first
  else
    Profiler.storage.load(token)
  end
  unless profile
    return [{ type: "text", text: "Profile not found: #{token}" }]
  end

  mailer_data = profile.collector_data("mailer")
  unless mailer_data && mailer_data["total"].to_i + mailer_data["queued_count"].to_i > 0
    return [{ type: "text", text: "No mailer activity found in this profile" }]
  end

  [{ type: "text", text: format_mailers(profile, mailer_data, params) }]
end

.filter_entries(entries, mailer_filter, action_filter, mode_filter) ⇒ Object



65
66
67
68
69
70
# File 'lib/profiler/mcp/tools/get_profile_mailers.rb', line 65

def self.filter_entries(entries, mailer_filter, action_filter, mode_filter)
  entries = entries.select { |e| e["mailer_class"].to_s.downcase.include?(mailer_filter) } if mailer_filter
  entries = entries.select { |e| e["action"].to_s.downcase.include?(action_filter) }       if action_filter
  entries = entries.select { |e| e["delivery_mode"].to_s == mode_filter }                  if mode_filter
  entries
end

.format_mailers(profile, mailer_data, params) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/profiler/mcp/tools/get_profile_mailers.rb', line 34

def self.format_mailers(profile, mailer_data, params)
  mailer_filter = params["mailer_class"]&.downcase
  action_filter = params["action"]&.downcase
  mode_filter   = params["delivery_mode"]

  emails  = filter_entries(mailer_data["emails"]  || [], mailer_filter, action_filter, mode_filter)
  errors  = filter_entries(mailer_data["errors"]  || [], mailer_filter, action_filter, mode_filter)
  queued  = filter_entries(mailer_data["queued"]  || [], mailer_filter, action_filter, mode_filter)

  lines = []
  lines << "# Mailer Activity: #{profile.token}\n"
  lines << "**Request:** #{profile.method} #{profile.path}"
  lines << "**Delivered:** #{mailer_data['total']} email(s)"
  lines << "**Queued (deliver_later):** #{mailer_data['queued_count']} email(s)"
  lines << "**Errors:** #{mailer_data['failed']}"
  lines << "**Body captured:** #{mailer_data.dig('emails', 0, 'body_captured') ? 'yes' : 'no (set capture_mail_body: true)'}"

  loop_warnings = mailer_data["loop_warnings"] || []
  if loop_warnings.any?
    lines << ""
    lines << "⚠️ **Loop warnings:**"
    loop_warnings.each { |w| lines << "  - #{w['message']}" }
  end

  append_email_section(lines, "Delivered Emails", emails, profile, params) if emails.any?
  append_email_section(lines, "Errors", errors, profile, params) if errors.any?
  append_queued_section(lines, queued) if queued.any?

  lines.join("\n")
end