Module: OmniAI::Tools::Browser::PageInspect::ButtonSummarizer

Defined in:
lib/omniai/tools/browser/page_inspect/button_summarizer.rb

Overview

Module to handle button elements summarization for AI agents

Class Method Summary collapse

Class Method Details

.critical_action?(button) ⇒ Boolean

Returns:

  • (Boolean)


100
101
102
103
104
105
# File 'lib/omniai/tools/browser/page_inspect/button_summarizer.rb', line 100

def critical_action?(button)
  text = get_button_text(button).downcase
  %w[save submit send create].any? do |keyword|
    text.include?(keyword)
  end
end

.distinctive_text?(button) ⇒ Boolean

Returns:

  • (Boolean)


132
133
134
135
# File 'lib/omniai/tools/browser/page_inspect/button_summarizer.rb', line 132

def distinctive_text?(button)
  text = get_button_text(button)
  text.length > 2 && text != "Button"
end

.find_primary_buttons(doc) ⇒ Object



18
19
20
21
# File 'lib/omniai/tools/browser/page_inspect/button_summarizer.rb', line 18

def find_primary_buttons(doc)
  all_buttons = doc.css('button, input[type="button"], input[type="submit"], [role="button"], [tabindex="0"]')
  all_buttons.select { |btn| primary_action?(btn) && !skip_button?(btn) }
end

.format_action_button(button) ⇒ Object



118
119
120
121
122
123
# File 'lib/omniai/tools/browser/page_inspect/button_summarizer.rb', line 118

def format_action_button(button)
  text = get_button_text(button)
  selector = get_button_selector(button)

  "#{text} (#{selector})\n"
end

.format_button_group(buttons, title) ⇒ Object



107
108
109
110
111
112
113
114
115
116
# File 'lib/omniai/tools/browser/page_inspect/button_summarizer.rb', line 107

def format_button_group(buttons, title)
  return "" if buttons.empty?

  result = "#{title}:\n"
  buttons.first(5).each do |btn|
    result += format_action_button(btn)
  end
  result += "  ... and #{buttons.size - 5} more\n" if buttons.size > 5
  result += "\n"
end

.format_primary_actions(buttons) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/omniai/tools/browser/page_inspect/button_summarizer.rb', line 87

def format_primary_actions(buttons)
  result = "⚡ Primary Actions:\n"

  # Group by importance
  critical = buttons.select(&method(:critical_action?))
  regular = buttons - critical

  result += format_button_group(critical, "🔥 Critical")
  result += format_button_group(regular, "📝 Actions")

  "#{result}\n"
end

.get_button_selector(button) ⇒ Object



125
126
127
128
129
130
# File 'lib/omniai/tools/browser/page_inspect/button_summarizer.rb', line 125

def get_button_selector(button)
  return button["id"] if button["id"] && !button["id"].empty?
  return "text:#{get_button_text(button)}" if distinctive_text?(button)

  "css-selector-needed"
end

.get_button_text(button) ⇒ Object



79
80
81
82
83
84
85
# File 'lib/omniai/tools/browser/page_inspect/button_summarizer.rb', line 79

def get_button_text(button)
  text = button.text.strip
  text = button["value"] if text.empty? && button["value"]
  text = button["aria-label"] if text.empty? && button["aria-label"]

  text.empty? ? "Button" : text
end

.primary_action?(button) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
32
33
34
35
36
# File 'lib/omniai/tools/browser/page_inspect/button_summarizer.rb', line 29

def primary_action?(button)
  return true if button["type"] == "submit"
  return true if primary_button_text?(button)
  return true if primary_button_class?(button)
  return true if workflow_action_button?(button)

  false
end

.primary_button_class?(button) ⇒ Boolean

Returns:

  • (Boolean)


53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/omniai/tools/browser/page_inspect/button_summarizer.rb', line 53

def primary_button_class?(button)
  classes = button["class"] || ""

  # Generic primary button patterns (universal)
  primary_classes = %w[primary submit btn-primary button--primary save continue]
  generic_match = primary_classes.any? { |css_class| classes.include?(css_class) }

  # Generic link-button patterns (works across frameworks)
  link_button_patterns = %w[button--link btn-link link-button button-link]
  link_match = link_button_patterns.any? { |pattern| classes.include?(pattern) }

  generic_match || link_match
end

.primary_button_text?(button) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/omniai/tools/browser/page_inspect/button_summarizer.rb', line 38

def primary_button_text?(button)
  text = get_button_text(button).downcase

  # Direct keyword matches
  primary_keywords = %w[save submit continue next finish send create update
                        delete cancel close done confirm proceed add edit]
  return true if primary_keywords.any? { |keyword| text.include?(keyword) }

  # Workflow action patterns
  return true if text.include?("add") && text.match?(/item|customer|discount|product|contact|line/)
  return true if text.include?("choose") || text.include?("select")

  false
end

.skip_button?(button) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
26
27
# File 'lib/omniai/tools/browser/page_inspect/button_summarizer.rb', line 23

def skip_button?(button)
  button["disabled"] ||
    button["style"]&.include?("display: none") ||
    button["aria-hidden"] == "true"
end

.summarize_primary_actions(doc) ⇒ Object



11
12
13
14
15
16
# File 'lib/omniai/tools/browser/page_inspect/button_summarizer.rb', line 11

def summarize_primary_actions(doc)
  buttons = find_primary_buttons(doc)
  return "" if buttons.empty?

  format_primary_actions(buttons)
end

.workflow_action_button?(button) ⇒ Boolean

Returns:

  • (Boolean)


67
68
69
70
71
72
73
74
75
76
77
# File 'lib/omniai/tools/browser/page_inspect/button_summarizer.rb', line 67

def workflow_action_button?(button)
  text = get_button_text(button).downcase

  # Check for common workflow patterns
  return true if text.match?(/add.*(item|customer|discount|product|contact)/i)
  return true if text.match?(/edit.*(column|field|profile)/i)
  return true if text.match?(/choose.*(different|customer)/i)
  return true if text.match?(/(create|new).*(item|customer|product)/i)

  false
end