Class: Rack::AI::Features::Moderation

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(provider, config) ⇒ Moderation



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

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

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



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

def config
  @config
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#providerObject (readonly)

Returns the value of attribute provider.



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

def provider
  @provider
end

Instance Method Details

#enabled?Boolean



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

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

#process_request(env) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/rack/ai/features/moderation.rb', line 25

def process_request(env)
  content = extract_content_from_request(env)
  return { processed: false, reason: "no_content" } if content.empty?

  result = @provider.moderate_content(content.join(" "))
  
  # Apply toxicity threshold
  threshold = get_toxicity_threshold
  max_score = result[:category_scores]&.values&.max || 0.0
  
  result[:action] = if result[:flagged] && max_score > threshold
                     should_block_on_violation? ? :block : :flag
                   else
                     :allow
                   end

  # Add metadata
  result[:feature] = @name
  result[:timestamp] = Time.now.iso8601
  result[:content_analyzed] = content.size
  result[:threshold] = threshold

  result
end

#process_response(env, status, headers, body) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/rack/ai/features/moderation.rb', line 50

def process_response(env, status, headers, body)
  return { processed: false, reason: "disabled" } unless process_response?
  
  content = extract_content_from_response(body)
  return { processed: false, reason: "no_content" } if content.empty?

  result = @provider.moderate_content(content)
  result[:feature] = "#{@name}_response"
  result[:timestamp] = Time.now.iso8601

  result
end

#process_response?Boolean



19
20
21
22
23
# File 'lib/rack/ai/features/moderation.rb', line 19

def process_response?
  @config.respond_to?(:moderation) && 
    @config.moderation.respond_to?(:check_response) && 
    @config.moderation.check_response
end