Module: Legion::Extensions::Llm::Ledger::Helpers::CallerIdentity
- Defined in:
- lib/legion/extensions/llm/ledger/helpers/caller_identity.rb
Constant Summary collapse
- GENERIC_IDENTITIES =
%w[anonymous process service system user].freeze
Class Method Summary collapse
- .first_present(*values) ⇒ Object
- .hash_value(hash, key) ⇒ Object
- .header_value(headers, key) ⇒ Object
- .normalize(caller_raw: nil, identity: nil, headers: {}) ⇒ Object
- .normalize_identity_value(value, type) ⇒ Object
- .present?(value) ⇒ Boolean
Class Method Details
.first_present(*values) ⇒ Object
56 57 58 |
# File 'lib/legion/extensions/llm/ledger/helpers/caller_identity.rb', line 56 def first_present(*values) values.find { |value| present?(value) } end |
.hash_value(hash, key) ⇒ Object
60 61 62 63 64 65 66 |
# File 'lib/legion/extensions/llm/ledger/helpers/caller_identity.rb', line 60 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 |
.header_value(headers, key) ⇒ Object
68 69 70 71 72 |
# File 'lib/legion/extensions/llm/ledger/helpers/caller_identity.rb', line 68 def header_value(headers, key) return nil unless headers.respond_to?(:key?) headers[key] || headers[key.to_sym] end |
.normalize(caller_raw: nil, identity: nil, headers: {}) ⇒ Object
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 |
# File 'lib/legion/extensions/llm/ledger/helpers/caller_identity.rb', line 13 def normalize(caller_raw: nil, identity: nil, headers: {}) caller_hash = caller_raw.is_a?(Hash) ? caller_raw : {} caller = hash_value(caller_hash, :requested_by) caller = caller_hash unless caller.is_a?(Hash) identity_hash = identity.is_a?(Hash) ? identity : {} extension = hash_value(caller_hash, :extension) type = first_present( hash_value(caller, :type), hash_value(identity_hash, :type), header_value(headers, 'x-legion-caller-type'), extension && 'extension' ) raw_identity = first_present( hash_value(caller, :id), hash_value(caller, :canonical_name), hash_value(identity_hash, :id), hash_value(identity_hash, :canonical_name), hash_value(identity_hash, :identity), hash_value(identity_hash, :username), header_value(headers, 'x-legion-identity'), header_value(headers, 'x-legion-caller-identity'), hash_value(caller, :identity), hash_value(caller, :username), extension && "extension:#{extension}" ) { identity: normalize_identity_value(raw_identity, type), type: type }.compact end |
.normalize_identity_value(value, type) ⇒ Object
46 47 48 49 50 51 52 53 54 |
# File 'lib/legion/extensions/llm/ledger/helpers/caller_identity.rb', line 46 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 |
.present?(value) ⇒ Boolean
74 75 76 |
# File 'lib/legion/extensions/llm/ledger/helpers/caller_identity.rb', line 74 def present?(value) !value.nil? && !(value.respond_to?(:empty?) && value.empty?) end |