Module: Legion::LLM::CallerIdentity
- Defined in:
- lib/legion/llm/caller_identity.rb
Constant Summary collapse
- GENERIC_IDENTITIES =
%w[anonymous process service system user].freeze
- DEFAULT_IDENTITY =
{ identity: 'unknown:anonymous', type: 'unknown' }.freeze
Class Method Summary collapse
- .first_present(*values) ⇒ Object
- .hash_value(hash, key) ⇒ Object
- .normalize(caller: nil, identity: nil) ⇒ Object
- .normalize_identity_option(value) ⇒ Object
- .normalize_identity_value(value, type) ⇒ Object
- .normalize_string(value) ⇒ Object
- .present?(value) ⇒ Boolean
Class Method Details
.first_present(*values) ⇒ Object
75 76 77 |
# File 'lib/legion/llm/caller_identity.rb', line 75 def first_present(*values) values.find { |value| present?(value) } end |
.hash_value(hash, key) ⇒ Object
79 80 81 82 83 84 85 |
# File 'lib/legion/llm/caller_identity.rb', line 79 def hash_value(hash, key) return nil unless hash.respond_to?(:key?) return hash[key] if hash.key?(key) string_key = key.to_s hash[string_key] if hash.key?(string_key) end |
.normalize(caller: nil, identity: nil) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/legion/llm/caller_identity.rb', line 11 def normalize(caller: nil, identity: nil) return normalize_string(caller) if caller.is_a?(String) caller_hash = caller.is_a?(Hash) ? caller : {} requested_by = hash_value(caller_hash, :requested_by) requested_by = caller_hash unless requested_by.is_a?(Hash) top_identity = normalize_identity_option(identity) extension = hash_value(caller_hash, :extension) type = first_present( hash_value(requested_by, :type), hash_value(top_identity, :type), extension && 'extension' ) raw_identity = first_present( hash_value(requested_by, :id), hash_value(requested_by, :canonical_name), hash_value(top_identity, :id), hash_value(top_identity, :canonical_name), hash_value(top_identity, :identity), hash_value(top_identity, :username), hash_value(requested_by, :identity), hash_value(requested_by, :username), extension && "extension:#{extension}" ) result = { identity: normalize_identity_value(raw_identity, type), type: type, credential: first_present(hash_value(requested_by, :credential), hash_value(top_identity, :credential)), hostname: hash_value(requested_by, :hostname) }.compact result.empty? ? DEFAULT_IDENTITY.dup : result end |
.normalize_identity_option(value) ⇒ Object
54 55 56 57 58 59 60 61 62 63 |
# File 'lib/legion/llm/caller_identity.rb', line 54 def normalize_identity_option(value) case value when Hash value when String { identity: value } else {} end end |
.normalize_identity_value(value, type) ⇒ Object
65 66 67 68 69 70 71 72 73 |
# File 'lib/legion/llm/caller_identity.rb', line 65 def normalize_identity_value(value, type) return nil unless present?(value) text = value.to_s return text if text.include?(':') || text.include?('@') return "#{type}:#{text}" if type && GENERIC_IDENTITIES.include?(text) text end |
.normalize_string(value) ⇒ Object
47 48 49 50 51 52 |
# File 'lib/legion/llm/caller_identity.rb', line 47 def normalize_string(value) return DEFAULT_IDENTITY.dup unless present?(value) type = value.to_s.include?(':') ? value.to_s.split(':', 2).first : nil { identity: value.to_s, type: type }.compact end |
.present?(value) ⇒ Boolean
87 88 89 |
# File 'lib/legion/llm/caller_identity.rb', line 87 def present?(value) !value.nil? && !(value.respond_to?(:empty?) && value.empty?) end |