Class: RubyLLM::TopSecret::Payload

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby_llm/top_secret/payload.rb

Overview

Represents the sensitive content flowing between a RubyLLM chat and the LLM provider. Mirrors Top Secret’s own vocabulary: #filter filters messages before sending, and #restore restores placeholders in the response.

Examples:

payload = Payload.new(messages)
payload.filter
# ... send to provider ...
restored_content = payload.restore(response.content)

Instance Method Summary collapse

Constructor Details

#initialize(messages) ⇒ Payload

Returns a new instance of Payload.

Parameters:

  • messages (Array<RubyLLM::Message>)


17
18
19
20
# File 'lib/ruby_llm/top_secret/payload.rb', line 17

def initialize(messages)
  @messages = messages
  @mapping = {}
end

Instance Method Details

#filtervoid

This method returns an undefined value.

Filters sensitive content from messages in place using TopSecret::Text.filter_all for consistent labels across turns.



25
26
27
28
29
30
31
32
# File 'lib/ruby_llm/top_secret/payload.rb', line 25

def filter
  filterable = @messages.reject { |msg| msg.content.nil? || msg.content.empty? }
  contents = filterable.map(&:content)
  batch_result = ::TopSecret::Text.filter_all(contents)

  filterable.zip(batch_result.items).each { |msg, item| msg.content = item.output }
  @mapping = batch_result.mapping
end

#restore(content) ⇒ String, Hash

Restores filtered placeholders in the response content with original values. Handles both String and Hash (structured output) content.

Parameters:

  • content (String, Hash)

    the response content to restore

Returns:

  • (String, Hash)

    content with placeholders replaced



39
40
41
42
43
44
45
46
47
# File 'lib/ruby_llm/top_secret/payload.rb', line 39

def restore(content)
  if content.is_a?(Hash)
    json = JSON.generate(content)
    restored = ::TopSecret::FilteredText.restore(json, mapping: @mapping)
    JSON.parse(restored.output)
  else
    ::TopSecret::FilteredText.restore(content, mapping: @mapping).output
  end
end