legion-llm 0.5.22 → 0.5.24
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +12 -0
- data/lib/legion/llm/daemon_client.rb +6 -3
- data/lib/legion/llm/hooks/reciprocity.rb +45 -0
- data/lib/legion/llm/hooks.rb +1 -0
- data/lib/legion/llm/pipeline/steps/gaia_advisory.rb +103 -0
- data/lib/legion/llm/routes.rb +13 -7
- data/lib/legion/llm/version.rb +1 -1
- metadata +2 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 7faa875219df27f00724444b192db231e6ef9f0b9a56da041791fbec1a17abb0
|
|
4
|
+
data.tar.gz: 271946a65933ed5366a9240490f4ba2df08f9a5c70200ab3b2605d413f6da611
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 5b75a996f84fa52a56e2dd145ae411007e7b0e82ebbf4c3ebf472d3368c8fb9647c311de67eb3500861c2758ba2c62ae7d90c7f8540c2080625ee99ba40d21c2
|
|
7
|
+
data.tar.gz: d390c9821b76cca8889c46a5f1999408de60dadb5d0c6db05c3fa7b0e68faa152bd557b9e19e38897b5ff52af8a05be26f74f0dc32a10ea09a282247a62f0c60
|
data/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,18 @@
|
|
|
2
2
|
|
|
3
3
|
## [Unreleased]
|
|
4
4
|
|
|
5
|
+
## [0.5.24] - 2026-03-31
|
|
6
|
+
|
|
7
|
+
### Added
|
|
8
|
+
- `DaemonClient.inference` accepts optional `caller:` and `conversation_id:` kwargs, forwarded in POST body
|
|
9
|
+
- `/api/llm/inference` route accepts `caller` and `conversation_id` from POST body, forwards to `Legion::LLM.chat`
|
|
10
|
+
|
|
11
|
+
## [0.5.23] - 2026-03-31
|
|
12
|
+
|
|
13
|
+
### Added
|
|
14
|
+
- `Hooks::Reciprocity` — after_chat hook that records a `:given` social exchange event via `Social::Social::Client#record_exchange` when a caller with identity receives an LLM response; silently no-ops when social extension or identity is absent
|
|
15
|
+
- Partner context enrichment in `Pipeline::Steps::GaiaAdvisory` (step 7) — when the caller identity is registered as a partner in `Legion::Gaia::BondRegistry`, the advisory data is enriched with a `:partner_context` hash containing standing, compatibility, recent_sentiment, and interaction_pattern; sourced from Apollo Local `partner`-tagged entries with full graceful degradation when Apollo is unavailable
|
|
16
|
+
|
|
5
17
|
## [0.5.22] - 2026-03-31
|
|
6
18
|
|
|
7
19
|
### Added
|
|
@@ -105,10 +105,13 @@ module Legion
|
|
|
105
105
|
# POSTs a conversation-level inference request to the daemon REST API.
|
|
106
106
|
# Accepts a full messages array and optional tool schemas.
|
|
107
107
|
# Returns a status hash with structured inference fields on success.
|
|
108
|
-
def inference(messages:, tools: [], model: nil, provider: nil,
|
|
108
|
+
def inference(messages:, tools: [], model: nil, provider: nil, caller: nil, conversation_id: nil,
|
|
109
|
+
timeout: 120)
|
|
109
110
|
body = { messages: messages, tools: tools }
|
|
110
|
-
body[:model]
|
|
111
|
-
body[:provider]
|
|
111
|
+
body[:model] = model if model
|
|
112
|
+
body[:provider] = provider if provider
|
|
113
|
+
body[:caller] = caller if caller
|
|
114
|
+
body[:conversation_id] = conversation_id if conversation_id
|
|
112
115
|
|
|
113
116
|
response = http_post('/api/llm/inference', body, timeout: timeout)
|
|
114
117
|
interpret_inference_response(response)
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Legion
|
|
4
|
+
module LLM
|
|
5
|
+
module Hooks
|
|
6
|
+
# Records a :given exchange event on the social graph whenever the LLM
|
|
7
|
+
# responds to a caller that carries an identity. Runs as an after_chat hook.
|
|
8
|
+
#
|
|
9
|
+
# The hook is intentionally lightweight — it does not block the response
|
|
10
|
+
# path and silently swallows all errors so a social-layer problem never
|
|
11
|
+
# surfaces to the caller.
|
|
12
|
+
module Reciprocity
|
|
13
|
+
module_function
|
|
14
|
+
|
|
15
|
+
def install
|
|
16
|
+
Legion::LLM::Hooks.after_chat do |caller: nil, **|
|
|
17
|
+
record_reciprocity(caller: caller)
|
|
18
|
+
nil
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def record_reciprocity(caller:)
|
|
23
|
+
identity = caller&.dig(:requested_by, :identity)
|
|
24
|
+
return unless identity
|
|
25
|
+
|
|
26
|
+
runner = social_runner
|
|
27
|
+
return unless runner
|
|
28
|
+
|
|
29
|
+
runner.record_exchange(agent_id: identity, action: :communication, direction: :given)
|
|
30
|
+
rescue StandardError => e
|
|
31
|
+
Legion::Logging.debug "[LLM::Reciprocity] hook error: #{e.message}" if defined?(Legion::Logging)
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def social_runner
|
|
35
|
+
return nil unless defined?(Legion::Extensions::Agentic::Social::Social::Client)
|
|
36
|
+
|
|
37
|
+
Legion::Extensions::Agentic::Social::Social::Client.new
|
|
38
|
+
rescue StandardError => e
|
|
39
|
+
Legion::Logging.debug "[LLM::Reciprocity] social_runner error: #{e.message}" if defined?(Legion::Logging)
|
|
40
|
+
nil
|
|
41
|
+
end
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
end
|
data/lib/legion/llm/hooks.rb
CHANGED
|
@@ -19,6 +19,8 @@ module Legion
|
|
|
19
19
|
|
|
20
20
|
return if advisory.nil? || advisory.empty?
|
|
21
21
|
|
|
22
|
+
enrich_advisory_with_partner_context(advisory)
|
|
23
|
+
|
|
22
24
|
@enrichments['gaia:advisory'] = {
|
|
23
25
|
content: advisory_summary(advisory),
|
|
24
26
|
data: advisory,
|
|
@@ -48,6 +50,29 @@ module Legion
|
|
|
48
50
|
@warnings << "GAIA advisory error: #{e.message}"
|
|
49
51
|
end
|
|
50
52
|
|
|
53
|
+
# Exposed as a public method so specs can stub it on instances.
|
|
54
|
+
def build_partner_context(identity)
|
|
55
|
+
return default_partner_context unless apollo_local_available?
|
|
56
|
+
|
|
57
|
+
entries = ::Legion::Apollo::Local.query(
|
|
58
|
+
text: identity,
|
|
59
|
+
tags: ['partner'],
|
|
60
|
+
limit: 5
|
|
61
|
+
)
|
|
62
|
+
|
|
63
|
+
results = entries.is_a?(Hash) ? (entries[:results] || []) : Array(entries)
|
|
64
|
+
|
|
65
|
+
{
|
|
66
|
+
standing: extract_standing(results),
|
|
67
|
+
compatibility: extract_compatibility(results),
|
|
68
|
+
recent_sentiment: extract_sentiment(results),
|
|
69
|
+
interaction_pattern: extract_interaction_pattern(results)
|
|
70
|
+
}
|
|
71
|
+
rescue StandardError => e
|
|
72
|
+
Legion::Logging.debug "[GaiaAdvisory] build_partner_context error: #{e.message}" if defined?(Legion::Logging)
|
|
73
|
+
nil
|
|
74
|
+
end
|
|
75
|
+
|
|
51
76
|
private
|
|
52
77
|
|
|
53
78
|
def advisory_summary(advisory)
|
|
@@ -57,6 +82,84 @@ module Legion
|
|
|
57
82
|
parts << "suppress:#{advisory[:suppress]&.join(',')}" if advisory[:suppress]
|
|
58
83
|
parts.empty? ? 'no enrichment' : parts.join(', ')
|
|
59
84
|
end
|
|
85
|
+
|
|
86
|
+
def enrich_advisory_with_partner_context(advisory)
|
|
87
|
+
return unless defined?(::Legion::Gaia::BondRegistry)
|
|
88
|
+
|
|
89
|
+
identity = @request.caller&.dig(:requested_by, :identity)
|
|
90
|
+
return unless identity
|
|
91
|
+
return unless ::Legion::Gaia::BondRegistry.partner?(identity)
|
|
92
|
+
|
|
93
|
+
partner_ctx = build_partner_context(identity)
|
|
94
|
+
advisory[:partner_context] = partner_ctx if partner_ctx
|
|
95
|
+
rescue StandardError => e
|
|
96
|
+
Legion::Logging.debug "[GaiaAdvisory] partner context error: #{e.message}" if defined?(Legion::Logging)
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
def apollo_local_available?
|
|
100
|
+
defined?(::Legion::Apollo::Local) && ::Legion::Apollo::Local.started?
|
|
101
|
+
rescue StandardError
|
|
102
|
+
false
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
def default_partner_context
|
|
106
|
+
{
|
|
107
|
+
standing: :unknown,
|
|
108
|
+
compatibility: nil,
|
|
109
|
+
recent_sentiment: :neutral,
|
|
110
|
+
interaction_pattern: :unknown
|
|
111
|
+
}
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
def extract_standing(results)
|
|
115
|
+
entry = results.find { |r| r[:content].to_s.match?(/standing/i) }
|
|
116
|
+
return :unknown unless entry
|
|
117
|
+
|
|
118
|
+
content = entry[:content].to_s
|
|
119
|
+
if content.match?(/good|trusted|positive/i)
|
|
120
|
+
:good
|
|
121
|
+
elsif content.match?(/poor|untrusted|negative/i)
|
|
122
|
+
:poor
|
|
123
|
+
else
|
|
124
|
+
:neutral
|
|
125
|
+
end
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
def extract_compatibility(results)
|
|
129
|
+
entry = results.find { |r| r[:content].to_s.match?(/compat/i) }
|
|
130
|
+
return nil unless entry
|
|
131
|
+
|
|
132
|
+
match = entry[:content].to_s.match(/(\d+(?:\.\d+)?)/)
|
|
133
|
+
match ? match[1].to_f : nil
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
def extract_sentiment(results)
|
|
137
|
+
entry = results.find { |r| r[:content].to_s.match?(/sentiment|empathy|affect/i) }
|
|
138
|
+
return :neutral unless entry
|
|
139
|
+
|
|
140
|
+
content = entry[:content].to_s
|
|
141
|
+
if content.match?(/positive|happy|pleasant/i)
|
|
142
|
+
:positive
|
|
143
|
+
elsif content.match?(/negative|unhappy|tense/i)
|
|
144
|
+
:negative
|
|
145
|
+
else
|
|
146
|
+
:neutral
|
|
147
|
+
end
|
|
148
|
+
end
|
|
149
|
+
|
|
150
|
+
def extract_interaction_pattern(results)
|
|
151
|
+
entry = results.find { |r| r[:content].to_s.match?(/interaction|memory|trace/i) }
|
|
152
|
+
return :unknown unless entry
|
|
153
|
+
|
|
154
|
+
content = entry[:content].to_s
|
|
155
|
+
if content.match?(/frequent|regular|daily/i)
|
|
156
|
+
:frequent
|
|
157
|
+
elsif content.match?(/occasional|sometimes/i)
|
|
158
|
+
:occasional
|
|
159
|
+
else
|
|
160
|
+
:infrequent
|
|
161
|
+
end
|
|
162
|
+
end
|
|
60
163
|
end
|
|
61
164
|
end
|
|
62
165
|
end
|
data/lib/legion/llm/routes.rb
CHANGED
|
@@ -289,10 +289,12 @@ module Legion
|
|
|
289
289
|
body = parse_request_body
|
|
290
290
|
validate_required!(body, :messages)
|
|
291
291
|
|
|
292
|
-
messages
|
|
293
|
-
raw_tools
|
|
294
|
-
model
|
|
295
|
-
provider
|
|
292
|
+
messages = body[:messages]
|
|
293
|
+
raw_tools = body[:tools]
|
|
294
|
+
model = body[:model]
|
|
295
|
+
provider = body[:provider]
|
|
296
|
+
caller_context = body[:caller]
|
|
297
|
+
conversation_id = body[:conversation_id]
|
|
296
298
|
|
|
297
299
|
unless messages.is_a?(Array)
|
|
298
300
|
halt 400, { 'Content-Type' => 'application/json' },
|
|
@@ -331,13 +333,17 @@ module Legion
|
|
|
331
333
|
{ role: ms[:role].to_s, content: ms[:content].to_s }
|
|
332
334
|
end
|
|
333
335
|
|
|
334
|
-
|
|
336
|
+
effective_caller = caller_context || { source: 'api', path: request.path }
|
|
337
|
+
chat_opts = {
|
|
335
338
|
messages: normalized_messages,
|
|
336
339
|
model: model,
|
|
337
340
|
provider: provider,
|
|
338
341
|
tools: tool_declarations,
|
|
339
|
-
caller:
|
|
340
|
-
|
|
342
|
+
caller: effective_caller
|
|
343
|
+
}
|
|
344
|
+
chat_opts[:conversation_id] = conversation_id if conversation_id
|
|
345
|
+
|
|
346
|
+
result = Legion::LLM.chat(**chat_opts)
|
|
341
347
|
|
|
342
348
|
if result.is_a?(Legion::LLM::Pipeline::Response)
|
|
343
349
|
raw_msg = result.message
|
data/lib/legion/llm/version.rb
CHANGED
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: legion-llm
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.5.
|
|
4
|
+
version: 0.5.24
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Esity
|
|
@@ -253,6 +253,7 @@ files:
|
|
|
253
253
|
- lib/legion/llm/hooks/cost_tracking.rb
|
|
254
254
|
- lib/legion/llm/hooks/metering.rb
|
|
255
255
|
- lib/legion/llm/hooks/rag_guard.rb
|
|
256
|
+
- lib/legion/llm/hooks/reciprocity.rb
|
|
256
257
|
- lib/legion/llm/hooks/reflection.rb
|
|
257
258
|
- lib/legion/llm/hooks/response_guard.rb
|
|
258
259
|
- lib/legion/llm/off_peak.rb
|