Class: ActiveAgent::InlinePreviewInterceptor

Inherits:
Object
  • Object
show all
Includes:
Base64
Defined in:
lib/active_agent/inline_preview_interceptor.rb

Overview

Active Agent InlinePreviewInterceptor

Implements a agent preview interceptor that converts image tag src attributes that use inline cid: style URLs to data: style URLs so that they are visible when previewing an HTML prompt in a web browser.

This interceptor is enabled by default. To disable it, delete it from the ActiveAgent::Base.preview_interceptors array:

ActiveAgent::Base.preview_interceptors.delete(ActiveAgent::InlinePreviewInterceptor)

Constant Summary collapse

PATTERN =
/src=(?:"cid:[^"]+"|'cid:[^']+')/i

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context) ⇒ InlinePreviewInterceptor

:nodoc:



26
27
28
# File 'lib/active_agent/inline_preview_interceptor.rb', line 26

def initialize(context) # :nodoc:
  @context = context
end

Class Method Details

.previewing_prompt(context) ⇒ Object

:nodoc:



22
23
24
# File 'lib/active_agent/inline_preview_interceptor.rb', line 22

def self.previewing_prompt(context) # :nodoc:
  new(context).transform!
end

Instance Method Details

#transform!Object

:nodoc:



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/active_agent/inline_preview_interceptor.rb', line 30

def transform! # :nodoc:
  return context if html_part.blank?

  html_part.body = html_part.decoded.gsub(PATTERN) do |match|
    if part = find_part(match[9..-2])
      %(src="#{data_url(part)}")
    else
      match
    end
  end

  context
end