Class: Rack::AI::Features::Enhancement

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/ai/features/enhancement.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(provider, config) ⇒ Enhancement

Returns a new instance of Enhancement.



9
10
11
12
13
# File 'lib/rack/ai/features/enhancement.rb', line 9

def initialize(provider, config)
  @name = :enhancement
  @provider = provider
  @config = config
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



7
8
9
# File 'lib/rack/ai/features/enhancement.rb', line 7

def config
  @config
end

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/rack/ai/features/enhancement.rb', line 7

def name
  @name
end

#providerObject (readonly)

Returns the value of attribute provider.



7
8
9
# File 'lib/rack/ai/features/enhancement.rb', line 7

def provider
  @provider
end

Instance Method Details

#enabled?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/rack/ai/features/enhancement.rb', line 15

def enabled?
  @config.feature_enabled?(:enhancement)
end

#process_request(env) ⇒ Object



23
24
25
26
27
28
29
30
31
32
# File 'lib/rack/ai/features/enhancement.rb', line 23

def process_request(env)
  return { processed: false, reason: "disabled" } unless enabled?

  # Enhancement is primarily a response-processing feature
  {
    feature: @name,
    timestamp: Time.now.iso8601,
    enhancement_ready: true
  }
end

#process_response(env, status, headers, body) ⇒ 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/rack/ai/features/enhancement.rb', line 34

def process_response(env, status, headers, body)
  return { processed: false, reason: "disabled" } unless enabled?
  return { processed: false, reason: "non_success_status" } unless (200..299).include?(status)

  content_type = headers["Content-Type"] || ""
  return { processed: false, reason: "unsupported_content_type" } unless enhanceable_content?(content_type)

  # Extract content from response body
  content = extract_content(body)
  return { processed: false, reason: "no_content" } if content.empty?

  # Determine enhancement type based on content and request context
  enhancement_type = determine_enhancement_type(env, content_type, content)
  return { processed: false, reason: "no_enhancement_needed" } unless enhancement_type

  begin
    # Apply AI enhancement
    enhancement_result = @provider.enhance_content(content, enhancement_type)
    
    # Update response body if enhancement was successful
    if enhancement_result[:enhanced_content] && 
       enhancement_result[:enhanced_content] != content
      update_response_body(body, enhancement_result[:enhanced_content])
      headers["X-AI-Enhanced"] = "true"
      headers["X-AI-Enhancement-Type"] = enhancement_type.to_s
    end

    {
      enhancement_applied: true,
      enhancement_type: enhancement_type,
      original_length: content.length,
      enhanced_length: enhancement_result[:enhanced_content]&.length || content.length,
      improvement_ratio: calculate_improvement_ratio(content, enhancement_result[:enhanced_content]),
      feature: "#{@name}_response",
      timestamp: Time.now.iso8601
    }
  rescue => e
    Utils::Logger.warn("Content enhancement failed", error: e.message, enhancement_type: enhancement_type)
    {
      enhancement_applied: false,
      error: e.message,
      feature: "#{@name}_response",
      timestamp: Time.now.iso8601
    }
  end
end

#process_response?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/rack/ai/features/enhancement.rb', line 19

def process_response?
  true
end