Class: PromptSanitizer::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/prompt_sanitizer/session.rb

Overview

Maintains a shared Vault across multiple anonymize/deanonymize calls.

The same PII value always maps to the same replacement token within a session, and LLM responses can be deanonymized back to the originals.

Usage:

sanitizer = PromptSanitizer::Sanitizer.new(mode: :smart)

# Single-use block (vault cleared on exit):
sanitizer.session(session_id: "user-42") do |sess|
clean    = sess.anonymize(user_prompt)
response = call_llm(clean)
puts sess.deanonymize(response)
end

# Long-lived (manage lifecycle yourself):
sess = sanitizer.session
loop { sess.anonymize(...) ... }
sess.reset

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sanitizer, session_id: nil) ⇒ Session



29
30
31
32
33
# File 'lib/prompt_sanitizer/session.rb', line 29

def initialize(sanitizer, session_id: nil)
  @sanitizer  = sanitizer
  @session_id = session_id
  @vault      = Vault.new
end

Instance Attribute Details

#session_idObject (readonly)

Returns the value of attribute session_id.



25
26
27
# File 'lib/prompt_sanitizer/session.rb', line 25

def session_id
  @session_id
end

#vaultObject (readonly)

Returns the value of attribute vault.



25
26
27
# File 'lib/prompt_sanitizer/session.rb', line 25

def vault
  @vault
end

Instance Method Details

#anonymize(text) ⇒ String

Sanitize text using the session's shared vault. Returns the redacted string.



40
41
42
# File 'lib/prompt_sanitizer/session.rb', line 40

def anonymize(text)
  @sanitizer._run(text, @vault, session_id: @session_id).text
end

#anonymize_with_result(text) ⇒ SanitizeResult

Like #anonymize but returns the full SanitizeResult.



48
49
50
# File 'lib/prompt_sanitizer/session.rb', line 48

def anonymize_with_result(text)
  @sanitizer._run(text, @vault, session_id: @session_id)
end

#deanonymize(text) ⇒ String

Restore vault tokens in text back to the original PII values. Pass the LLM's response here to get a human-readable output.



57
58
59
# File 'lib/prompt_sanitizer/session.rb', line 57

def deanonymize(text)
  @vault.restore(text)
end

#inspectObject Also known as: to_s



92
93
94
# File 'lib/prompt_sanitizer/session.rb', line 92

def inspect
  "#<PromptSanitizer::Session id=#{@session_id.inspect} mappings=#{size}>"
end

#mappingHash{String => String}

Snapshot of original → replacement mapping for this session.



64
65
66
# File 'lib/prompt_sanitizer/session.rb', line 64

def mapping
  @vault.snapshot
end

#resetObject

Clear the vault, resetting the session for a fresh conversation.



76
77
78
# File 'lib/prompt_sanitizer/session.rb', line 76

def reset
  @vault.clear
end

#sizeInteger

Number of unique PII values currently stored in the session vault.



71
72
73
# File 'lib/prompt_sanitizer/session.rb', line 71

def size
  @vault.snapshot.size
end

#use {|session| ... } ⇒ Object

Block form — vault is cleared after the block returns.

sanitizer.session { |s| s.anonymize(...) }

Yield Parameters:



86
87
88
89
90
# File 'lib/prompt_sanitizer/session.rb', line 86

def use
  yield self
ensure
  reset
end