Module: Legion::LLM::PublisherIdentity

Defined in:
lib/legion/llm/publisher_identity.rb

Constant Summary collapse

GENERIC_PUBLISHER_IDENTITIES =
%w[
  anonymous process:anonymous service:system system system:system unknown:anonymous
].freeze

Class Method Summary collapse

Class Method Details

.caller_hashObject



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/legion/llm/publisher_identity.rb', line 22

def caller_hash
  identity = current
  {
    requested_by: {
      identity:   identity[:identity],
      type:       identity[:type],
      credential: identity[:credential],
      hostname:   identity[:hostname]
    }.compact
  }
end

.currentObject



14
15
16
17
18
19
20
# File 'lib/legion/llm/publisher_identity.rb', line 14

def current
  process = process_identity_module
  identity = process_identity(process)
  return identity if present_identity?(identity)

  env_identity
end

.env_identityObject



78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/legion/llm/publisher_identity.rb', line 78

def env_identity
  raw = ENV.fetch('USER', nil) || ENV.fetch('LOGNAME', nil)
  return CallerIdentity::DEFAULT_IDENTITY.dup unless present?(raw)

  CallerIdentity.normalize(
    caller: {
      requested_by: {
        identity:   raw.to_s,
        type:       :human,
        credential: :system
      }
    }
  )
end

.generic_requested_by?(value) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
41
42
43
44
45
46
# File 'lib/legion/llm/publisher_identity.rb', line 38

def generic_requested_by?(value)
  requested = value.is_a?(Hash) ? value : {}
  raw_id = hash_value(requested, :id).to_s
  return true if raw_id == 'system:system'

  identity = CallerIdentity.normalize(caller: { requested_by: requested })
  normalized = identity[:identity].to_s
  GENERIC_PUBLISHER_IDENTITIES.include?(normalized)
end

.hash_value(hash, key) ⇒ Object



101
102
103
104
105
106
107
# File 'lib/legion/llm/publisher_identity.rb', line 101

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

.present?(value) ⇒ Boolean

Returns:

  • (Boolean)


113
114
115
# File 'lib/legion/llm/publisher_identity.rb', line 113

def present?(value)
  !value.nil? && !(value.respond_to?(:empty?) && value.empty?)
end

.present_identity?(identity) ⇒ Boolean

Returns:

  • (Boolean)


109
110
111
# File 'lib/legion/llm/publisher_identity.rb', line 109

def present_identity?(identity)
  identity.is_a?(Hash) && present?(identity[:identity])
end

.process_identity(process) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/legion/llm/publisher_identity.rb', line 60

def process_identity(process)
  return nil unless process

  canonical = process_value(process, :canonical_name)
  return nil unless present?(canonical)

  CallerIdentity.normalize(
    caller: {
      requested_by: {
        identity:   canonical,
        type:       process_value(process, :kind) || :process,
        credential: process_value(process, :source) || :system,
        hostname:   process_value(process, :hostname)
      }.compact
    }
  )
end

.process_identity_moduleObject



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/legion/llm/publisher_identity.rb', line 48

def process_identity_module
  return Legion::Identity::Process if defined?(Legion::Identity::Process)

  begin
    require 'legion/identity/process'
  rescue LoadError
    nil
  end

  defined?(Legion::Identity::Process) ? Legion::Identity::Process : nil
end

.process_value(process, method_name) ⇒ Object



93
94
95
96
97
98
99
# File 'lib/legion/llm/publisher_identity.rb', line 93

def process_value(process, method_name)
  return nil unless process.respond_to?(method_name)

  process.public_send(method_name)
rescue StandardError
  nil
end

.requested_byObject



34
35
36
# File 'lib/legion/llm/publisher_identity.rb', line 34

def requested_by
  caller_hash[:requested_by]
end