Module: AI

Defined in:
lib/ai-chat.rb,
lib/ai/chat.rb,
lib/ai/items.rb,
lib/ai/message.rb

Defined Under Namespace

Classes: Chat, Items, Message

Constant Summary collapse

HTML_PRE_STYLE =
"background-color: #1e1e1e; color: #d4d4d4; padding: 1em; " \
"border-radius: 4px; overflow-x: auto; " \
"white-space: pre-wrap; word-wrap: break-word;"

Class Method Summary collapse

Class Method Details

.amazing_print(object, **options) ⇒ Object

Use AmazingPrint::Inspector directly to avoid conflicts with awesome_print gem which also defines an ‘ai` method on Object



13
14
15
# File 'lib/ai-chat.rb', line 13

def self.amazing_print(object, **options)
  AmazingPrint::Inspector.new(**options).awesome(object)
end

.truncate_data_uri(str) ⇒ Object



31
32
33
34
35
36
37
38
39
40
# File 'lib/ai-chat.rb', line 31

def self.truncate_data_uri(str)
  return str unless str.is_a?(String) && str.start_with?("data:") && str.include?(";base64,")

  match = str.match(/\A(data:[^;]+;base64,)(.+)\z/)
  return str unless match

  prefix = match[1]
  data = match[2]
  "#{prefix}#{data[0, 20]}... (#{data.length} chars)"
end

.truncate_data_uris(obj) ⇒ Object

Recursively truncate base64 data URIs in nested structures for cleaner output



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/ai-chat.rb', line 18

def self.truncate_data_uris(obj)
  case obj
  when Hash
    obj.transform_values { |v| truncate_data_uris(v) }
  when Array
    obj.map { |v| truncate_data_uris(v) }
  when String
    truncate_data_uri(obj)
  else
    obj
  end
end

.wrap_html(content) ⇒ Object



6
7
8
9
# File 'lib/ai-chat.rb', line 6

def self.wrap_html(content)
  html = "<pre style=\"#{HTML_PRE_STYLE}\">#{content}</pre>"
  html.respond_to?(:html_safe) ? html.html_safe : html
end