Class: PromptSanitizer::Session
- Inherits:
-
Object
- Object
- PromptSanitizer::Session
- 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
-
#session_id ⇒ Object
readonly
Returns the value of attribute session_id.
-
#vault ⇒ Object
readonly
Returns the value of attribute vault.
Instance Method Summary collapse
-
#anonymize(text) ⇒ String
Sanitize
textusing the session's shared vault. -
#anonymize_with_result(text) ⇒ SanitizeResult
Like #anonymize but returns the full SanitizeResult.
-
#deanonymize(text) ⇒ String
Restore vault tokens in
textback to the original PII values. -
#initialize(sanitizer, session_id: nil) ⇒ Session
constructor
A new instance of Session.
- #inspect ⇒ Object (also: #to_s)
-
#mapping ⇒ Hash{String => String}
Snapshot of original → replacement mapping for this session.
-
#reset ⇒ Object
Clear the vault, resetting the session for a fresh conversation.
-
#size ⇒ Integer
Number of unique PII values currently stored in the session vault.
-
#use {|session| ... } ⇒ Object
Block form — vault is cleared after the block returns.
Constructor Details
Instance Attribute Details
#session_id ⇒ Object (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 |
#vault ⇒ Object (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 |
#inspect ⇒ Object 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 |
#mapping ⇒ Hash{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 |
#reset ⇒ Object
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 |
#size ⇒ Integer
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(...) }
86 87 88 89 90 |
# File 'lib/prompt_sanitizer/session.rb', line 86 def use yield self ensure reset end |