Module: Legion::LLM::Hooks::Reciprocity

Extended by:
Legion::Logging::Helper
Defined in:
lib/legion/llm/hooks/reciprocity.rb

Overview

Records a :given exchange event on the social graph whenever the LLM responds to a caller that carries an identity. Runs as an after_chat hook.

The hook is intentionally lightweight — it does not block the response path and silently swallows all errors so a social-layer problem never surfaces to the caller.

Class Method Summary collapse

Class Method Details

.installObject



18
19
20
21
22
23
# File 'lib/legion/llm/hooks/reciprocity.rb', line 18

def install
  Legion::LLM::Hooks.after_chat do |caller: nil, **|
    record_reciprocity(caller: caller)
    nil
  end
end

.record_reciprocity(caller:) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/legion/llm/hooks/reciprocity.rb', line 25

def record_reciprocity(caller:)
  identity = caller&.dig(:requested_by, :identity)
  return unless identity

  runner = social_runner
  return unless runner

  runner.record_exchange(agent_id: identity, action: :communication, direction: :given)
rescue StandardError => e
  handle_exception(e, level: :debug)
end

.social_runnerObject



37
38
39
40
41
42
43
44
# File 'lib/legion/llm/hooks/reciprocity.rb', line 37

def social_runner
  return nil unless defined?(Legion::Extensions::Agentic::Social::Social::Client)

  Legion::Extensions::Agentic::Social::Social::Client.new
rescue StandardError => e
  handle_exception(e, level: :debug)
  nil
end