Module: Legion::Extensions::Mesh::Runners::Preferences

Includes:
Helpers::Lex
Included in:
Client
Defined in:
lib/legion/extensions/mesh/runners/preferences.rb

Instance Method Summary collapse

Instance Method Details

#dispatch_preference_message(type: nil, **msg) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/legion/extensions/mesh/runners/preferences.rb', line 65

def dispatch_preference_message(type: nil, **msg)
  case type
  when 'preference_query'
    profile = handle_preference_query(**msg)
    publish_preference_response(msg, profile) if profile[:success]
    profile
  when 'preference_response'
    handle_preference_response(
      correlation_id:      msg[:correlation_id],
      profile:             msg[:profile] || {},
      responding_agent_id: msg[:responding_agent_id]
    )
  else
    { success: false, error: "unknown preference message type: #{type}" }
  end
end

#expire_pending_requestsObject



60
61
62
63
# File 'lib/legion/extensions/mesh/runners/preferences.rb', line 60

def expire_pending_requests(**)
  expired = pending_requests.expire
  { expired: expired.size, correlation_ids: expired }
end

#handle_preference_query(requesting_agent_id: nil) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/legion/extensions/mesh/runners/preferences.rb', line 33

def handle_preference_query(requesting_agent_id: nil, **)
  if trust_available? && requesting_agent_id
    trust_result = check_requester_trust(requesting_agent_id)
    return { success: false, reason: :insufficient_trust, responding_agent_id: local_agent_id } if trust_result == :denied
  end

  owner_id = local_agent_id
  profile = Helpers::PreferenceProfile.resolve(owner_id: owner_id)

  { success: true, profile: profile, responding_agent_id: local_agent_id }
rescue StandardError => e
  { success: false, error: e.message }
end

#handle_preference_response(correlation_id:, profile:, responding_agent_id: nil) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/legion/extensions/mesh/runners/preferences.rb', line 47

def handle_preference_response(correlation_id:, profile:, responding_agent_id: nil, **)
  if responding_agent_id && profile.is_a?(Hash)
    Helpers::PreferenceProfile.store_mesh_profile(
      agent_id:        responding_agent_id,
      profile:         profile,
      source_agent_id: responding_agent_id
    )
  end

  resolved = pending_requests.resolve(correlation_id: correlation_id, result: profile)
  { resolved: resolved }
end

#query_preferences(target_agent_id:, domains: nil, callback: nil, ttl: 5) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/legion/extensions/mesh/runners/preferences.rb', line 13

def query_preferences(target_agent_id:, domains: nil, callback: nil, ttl: 5, **)
  default_profile = Helpers::PreferenceProfile.resolve(owner_id: target_agent_id)

  return { success: true, source: :local_default, profile: default_profile } unless transport_available?

  correlation_id = SecureRandom.uuid
  cb = callback || default_preference_callback(target_agent_id: target_agent_id)
  pending_requests.register(correlation_id: correlation_id, callback: cb, ttl: ttl)

  publish_preference_query(
    target_agent_id: target_agent_id,
    correlation_id:  correlation_id,
    domains:         domains
  )

  { success: true, source: :pending, correlation_id: correlation_id, profile: default_profile }
rescue StandardError => e
  { success: true, source: :local_default, profile: default_profile, error: e.message }
end