legionio 1.7.29 → 1.7.30
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 +13 -0
- data/lib/legion/api/llm.rb +80 -8
- data/lib/legion/cli/admin_command.rb +12 -8
- data/lib/legion/cli/chat/daemon_chat.rb +26 -8
- data/lib/legion/cli/groups/admin_group.rb +14 -0
- data/lib/legion/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 537bbc9c431e5e8b9fdaf96145ea124a0c4b8e84b4094673cbc4f6b07d4edb21
|
|
4
|
+
data.tar.gz: da76364c12123a27f644f4fdef175c14796b2542800a93b054b26cb5f27a5e79
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: bee0c19f368dffa21a46fc964a43ae7448356912cbc5f0ac6884c9bcd4e00660821bc66c37e2b8d2cc2e05c77ec0fbce17d2f5a6c70b5b2bfb0d5eb4c3c278a6
|
|
7
|
+
data.tar.gz: 60f48041416adb178c7aae384647c7fb15860aa2a25dde1418500ef947ac323ebf36340ab242e9abcdd2893e717e3bdef94bd62aea43955e927c26acf377aecf
|
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,18 @@
|
|
|
1
1
|
# Legion Changelog
|
|
2
2
|
|
|
3
|
+
## [1.7.30] - 2026-04-08
|
|
4
|
+
|
|
5
|
+
### Added
|
|
6
|
+
- SSE streaming inference now emits real-time `tool-call`, `tool-result`, `tool-error`, and `model-fallback` events via `executor.tool_event_handler` as tools execute (with wall-clock `startedAt`/`finishedAt`/`durationMs` timing)
|
|
7
|
+
- `event: done` payload extended with `conversation_id`, `stop_reason`, `cache_read_tokens`, and `cache_write_tokens` fields (nil values compacted out)
|
|
8
|
+
- Post-hoc `model-fallback` events emitted from `pipeline_response.warnings` for non-streaming tool paths
|
|
9
|
+
- `admin purge-topology` CLI command to remove stale v2.0 `legion.*` AMQP exchanges that have `lex.*` counterparts
|
|
10
|
+
- Parallel tool execution in `CLI::Chat::DaemonChat`: all tools in a response now run concurrently via `Thread.new`, preserving original order for message replay
|
|
11
|
+
- `build_tool_result_object` now carries `tool_call_id`/`id` so the Interlink frontend can match results to tool calls by ID rather than name (fixes parallel same-type tool matching)
|
|
12
|
+
|
|
13
|
+
### Changed
|
|
14
|
+
- SSE tool-call events now use camelCase keys (`toolCallId`, `toolName`, `args`) matching the Interlink wire protocol
|
|
15
|
+
|
|
3
16
|
## [1.7.29] - 2026-04-07
|
|
4
17
|
|
|
5
18
|
### Changed
|
data/lib/legion/api/llm.rb
CHANGED
|
@@ -306,6 +306,51 @@ module Legion
|
|
|
306
306
|
'X-Accel-Buffering' => 'no'
|
|
307
307
|
|
|
308
308
|
stream do |out|
|
|
309
|
+
# Wire up real-time tool-call / tool-result / tool-error / model-fallback SSE events.
|
|
310
|
+
# The executor fires tool_event_handler for each event as it happens,
|
|
311
|
+
# including accurate wall-clock startedAt/finishedAt/durationMs timing.
|
|
312
|
+
emitted_tool_call_ids = Set.new
|
|
313
|
+
executor.tool_event_handler = lambda do |event|
|
|
314
|
+
case event[:type]
|
|
315
|
+
when :tool_call
|
|
316
|
+
emitted_tool_call_ids << event[:tool_call_id] if event[:tool_call_id]
|
|
317
|
+
out << "event: tool-call\ndata: #{Legion::JSON.dump({
|
|
318
|
+
toolCallId: event[:tool_call_id],
|
|
319
|
+
toolName: event[:tool_name],
|
|
320
|
+
args: event[:arguments] || {},
|
|
321
|
+
startedAt: event[:started_at]&.iso8601(3),
|
|
322
|
+
timestamp: event[:started_at]&.iso8601(3) || Time.now.iso8601(3)
|
|
323
|
+
})}\n\n"
|
|
324
|
+
when :tool_result
|
|
325
|
+
out << "event: tool-result\ndata: #{Legion::JSON.dump({
|
|
326
|
+
toolCallId: event[:tool_call_id],
|
|
327
|
+
toolName: event[:tool_name],
|
|
328
|
+
result: event[:result],
|
|
329
|
+
startedAt: event[:started_at]&.iso8601(3),
|
|
330
|
+
finishedAt: event[:finished_at]&.iso8601(3) || Time.now.iso8601(3),
|
|
331
|
+
durationMs: event[:duration_ms],
|
|
332
|
+
timestamp: event[:finished_at]&.iso8601(3) || Time.now.iso8601(3)
|
|
333
|
+
})}\n\n"
|
|
334
|
+
when :tool_error
|
|
335
|
+
out << "event: tool-error\ndata: #{Legion::JSON.dump({
|
|
336
|
+
toolCallId: event[:tool_call_id],
|
|
337
|
+
toolName: event[:tool_name],
|
|
338
|
+
error: (event[:error] || event[:result]).to_s,
|
|
339
|
+
startedAt: event[:started_at]&.iso8601(3),
|
|
340
|
+
finishedAt: Time.now.iso8601(3),
|
|
341
|
+
timestamp: Time.now.iso8601(3)
|
|
342
|
+
})}\n\n"
|
|
343
|
+
when :model_fallback
|
|
344
|
+
out << "event: model-fallback\ndata: #{Legion::JSON.dump({
|
|
345
|
+
fromModel: event[:from_model],
|
|
346
|
+
toModel: event[:to_model],
|
|
347
|
+
toModelKey: event[:to_model],
|
|
348
|
+
error: event[:error] || 'Provider unavailable',
|
|
349
|
+
reason: event[:reason] || 'provider_fallback'
|
|
350
|
+
})}\n\n"
|
|
351
|
+
end
|
|
352
|
+
end
|
|
353
|
+
|
|
309
354
|
full_text = +''
|
|
310
355
|
pipeline_response = executor.call_stream do |chunk|
|
|
311
356
|
text = chunk.respond_to?(:content) ? chunk.content.to_s : chunk.to_s
|
|
@@ -315,26 +360,53 @@ module Legion
|
|
|
315
360
|
out << "event: text-delta\ndata: #{Legion::JSON.dump({ delta: text })}\n\n"
|
|
316
361
|
end
|
|
317
362
|
|
|
363
|
+
# Post-hoc safety net: emit any tool-calls that weren't fired in real-time
|
|
364
|
+
# (e.g. non-streaming tool paths). Skip IDs already sent via tool_event_handler.
|
|
318
365
|
if pipeline_response.tools.is_a?(Array) && !pipeline_response.tools.empty?
|
|
319
366
|
pipeline_response.tools.each do |tc|
|
|
367
|
+
tc_id = tc.respond_to?(:id) ? tc.id : nil
|
|
368
|
+
next if tc_id && emitted_tool_call_ids.include?(tc_id)
|
|
369
|
+
|
|
320
370
|
out << "event: tool-call\ndata: #{Legion::JSON.dump({
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
371
|
+
toolCallId: tc_id,
|
|
372
|
+
toolName: tc.respond_to?(:name) ? tc.name : tc.to_s,
|
|
373
|
+
args: tc.respond_to?(:arguments) ? tc.arguments : {}
|
|
324
374
|
})}\n\n"
|
|
325
375
|
end
|
|
326
376
|
end
|
|
327
377
|
|
|
378
|
+
# Emit any model-fallback warnings collected post-hoc
|
|
379
|
+
Array(pipeline_response.warnings).each do |w|
|
|
380
|
+
next unless w.is_a?(Hash) && w[:type] == :provider_fallback
|
|
381
|
+
|
|
382
|
+
fallback = w[:fallback].to_s
|
|
383
|
+
provider, model = fallback.split(':', 2)
|
|
384
|
+
resolved_model = (model || provider).to_s.strip
|
|
385
|
+
next if resolved_model.empty?
|
|
386
|
+
|
|
387
|
+
out << "event: model-fallback\ndata: #{Legion::JSON.dump({
|
|
388
|
+
fromModel: pipeline_response.routing&.dig(:model),
|
|
389
|
+
toModel: resolved_model,
|
|
390
|
+
toModelKey: resolved_model,
|
|
391
|
+
error: w[:original_error] || 'Provider unavailable',
|
|
392
|
+
reason: 'provider_fallback'
|
|
393
|
+
})}\n\n"
|
|
394
|
+
end
|
|
395
|
+
|
|
328
396
|
enrichments = pipeline_response.enrichments
|
|
329
397
|
out << "event: enrichment\ndata: #{Legion::JSON.dump(enrichments)}\n\n" if enrichments.is_a?(Hash) && !enrichments.empty?
|
|
330
398
|
|
|
331
399
|
tokens = pipeline_response.tokens
|
|
332
400
|
out << "event: done\ndata: #{Legion::JSON.dump({
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
401
|
+
content: full_text,
|
|
402
|
+
model: pipeline_response.routing&.dig(:model),
|
|
403
|
+
conversation_id: pipeline_response.conversation_id,
|
|
404
|
+
stop_reason: pipeline_response.stop&.dig(:reason)&.to_s,
|
|
405
|
+
input_tokens: tokens.respond_to?(:input_tokens) ? tokens.input_tokens : nil,
|
|
406
|
+
output_tokens: tokens.respond_to?(:output_tokens) ? tokens.output_tokens : nil,
|
|
407
|
+
cache_read_tokens: tokens.respond_to?(:cache_read_tokens) ? tokens.cache_read_tokens : nil,
|
|
408
|
+
cache_write_tokens: tokens.respond_to?(:cache_write_tokens) ? tokens.cache_write_tokens : nil
|
|
409
|
+
}.compact)}\n\n"
|
|
338
410
|
rescue StandardError => e
|
|
339
411
|
Legion::Logging.log_exception(e, payload_summary: 'api/llm/inference stream failed', component_type: :api)
|
|
340
412
|
out << "event: error\ndata: #{Legion::JSON.dump({ code: 'stream_error', message: e.message })}\n\n"
|
|
@@ -9,13 +9,15 @@ module Legion
|
|
|
9
9
|
namespace :admin
|
|
10
10
|
|
|
11
11
|
desc 'purge-topology', 'Remove old v2.0 AMQP exchanges (legion.* that have lex.* counterparts)'
|
|
12
|
-
method_option :dry_run,
|
|
13
|
-
method_option :execute,
|
|
14
|
-
method_option :host,
|
|
15
|
-
method_option :port,
|
|
16
|
-
method_option :user,
|
|
17
|
-
method_option :password,
|
|
18
|
-
method_option :vhost,
|
|
12
|
+
method_option :dry_run, type: :boolean, default: true, desc: 'List without deleting'
|
|
13
|
+
method_option :execute, type: :boolean, default: false, desc: 'Actually delete exchanges'
|
|
14
|
+
method_option :host, type: :string, default: 'localhost', desc: 'RabbitMQ management host'
|
|
15
|
+
method_option :port, type: :numeric, default: 15_672, desc: 'RabbitMQ management port'
|
|
16
|
+
method_option :user, type: :string, default: 'guest', desc: 'RabbitMQ management user'
|
|
17
|
+
method_option :password, type: :string, default: 'guest', desc: 'RabbitMQ management password'
|
|
18
|
+
method_option :vhost, type: :string, default: '/', desc: 'RabbitMQ vhost'
|
|
19
|
+
method_option :open_timeout, type: :numeric, default: 5, desc: 'HTTP open timeout in seconds'
|
|
20
|
+
method_option :read_timeout, type: :numeric, default: 30, desc: 'HTTP read timeout in seconds'
|
|
19
21
|
def purge_topology
|
|
20
22
|
exchanges = fetch_exchanges
|
|
21
23
|
candidates = self.class.detect_old_exchanges(exchanges)
|
|
@@ -76,7 +78,9 @@ module Legion
|
|
|
76
78
|
end
|
|
77
79
|
|
|
78
80
|
def management_request(uri, method_class)
|
|
79
|
-
Net::HTTP.start(uri.host, uri.port
|
|
81
|
+
Net::HTTP.start(uri.host, uri.port,
|
|
82
|
+
open_timeout: options[:open_timeout],
|
|
83
|
+
read_timeout: options[:read_timeout]) do |http|
|
|
80
84
|
req = method_class.new(uri)
|
|
81
85
|
req.basic_auth(options[:user], options[:password])
|
|
82
86
|
http.request(req)
|
|
@@ -32,6 +32,10 @@ module Legion
|
|
|
32
32
|
end
|
|
33
33
|
end
|
|
34
34
|
|
|
35
|
+
# Single shared struct class for tool result objects; avoids allocating
|
|
36
|
+
# an anonymous Struct class on every build_tool_result_object call.
|
|
37
|
+
ToolResult = Struct.new(:content, :tool_call_id, :id)
|
|
38
|
+
|
|
35
39
|
attr_reader :model, :conversation_id, :caller_context
|
|
36
40
|
|
|
37
41
|
def initialize(model: nil, provider: nil)
|
|
@@ -168,15 +172,24 @@ module Legion
|
|
|
168
172
|
# Record the assistant turn with tool_calls before appending results.
|
|
169
173
|
@messages << { role: 'assistant', content: assistant_content, tool_calls: tool_calls }
|
|
170
174
|
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
175
|
+
# Normalize all tool calls upfront so threads don't mutate shared state
|
|
176
|
+
normalized = tool_calls.map do |tc|
|
|
177
|
+
tc.respond_to?(:transform_keys) ? tc.transform_keys(&:to_sym) : tc
|
|
178
|
+
end
|
|
174
179
|
|
|
175
|
-
|
|
180
|
+
# Fire on_tool_call callbacks immediately (serial — fast, just event emission)
|
|
181
|
+
normalized.each do |tc|
|
|
182
|
+
@on_tool_call&.call(build_tool_call_object(tc))
|
|
183
|
+
end
|
|
176
184
|
|
|
177
|
-
|
|
185
|
+
# Execute all tools in parallel, preserving original order for message replay
|
|
186
|
+
results = normalized.map do |tc|
|
|
187
|
+
Thread.new { [tc, run_tool(tc)] }
|
|
188
|
+
end.map(&:value)
|
|
178
189
|
|
|
179
|
-
|
|
190
|
+
# Collect results serially: fire callbacks and append messages in order
|
|
191
|
+
results.each do |tc, result_text|
|
|
192
|
+
result_obj = build_tool_result_object(result_text, tc[:id] || tc[:tool_call_id])
|
|
180
193
|
@on_tool_result&.call(result_obj)
|
|
181
194
|
|
|
182
195
|
@messages << {
|
|
@@ -195,8 +208,13 @@ module Legion
|
|
|
195
208
|
)
|
|
196
209
|
end
|
|
197
210
|
|
|
198
|
-
|
|
199
|
-
|
|
211
|
+
# Carries both the result content AND the originating tool_call_id so the
|
|
212
|
+
# daemon-bridge-script serializer can include it in the tool-result event,
|
|
213
|
+
# allowing the Interlink frontend to match results back to the correct
|
|
214
|
+
# tool call by ID (rather than falling back to name-based matching which
|
|
215
|
+
# breaks when multiple tools of the same type run in parallel).
|
|
216
|
+
def build_tool_result_object(text, tool_call_id = nil)
|
|
217
|
+
ToolResult.new(text.to_s, tool_call_id, tool_call_id)
|
|
200
218
|
end
|
|
201
219
|
|
|
202
220
|
def run_tool(tool_call)
|
|
@@ -23,6 +23,20 @@ module Legion
|
|
|
23
23
|
|
|
24
24
|
desc 'team SUBCOMMAND', 'Team and multi-user management'
|
|
25
25
|
subcommand 'team', Legion::CLI::Team
|
|
26
|
+
|
|
27
|
+
desc 'purge-topology', 'Remove old v2.0 AMQP exchanges (legion.* that have lex.* counterparts)'
|
|
28
|
+
method_option :dry_run, type: :boolean, default: true, desc: 'List without deleting'
|
|
29
|
+
method_option :execute, type: :boolean, default: false, desc: 'Actually delete exchanges'
|
|
30
|
+
method_option :host, type: :string, default: 'localhost', desc: 'RabbitMQ management host'
|
|
31
|
+
method_option :port, type: :numeric, default: 15_672, desc: 'RabbitMQ management port'
|
|
32
|
+
method_option :user, type: :string, default: 'guest', desc: 'RabbitMQ management user'
|
|
33
|
+
method_option :password, type: :string, default: 'guest', desc: 'RabbitMQ management password'
|
|
34
|
+
method_option :vhost, type: :string, default: '/', desc: 'RabbitMQ vhost'
|
|
35
|
+
method_option :open_timeout, type: :numeric, default: 5, desc: 'HTTP open timeout in seconds'
|
|
36
|
+
method_option :read_timeout, type: :numeric, default: 30, desc: 'HTTP read timeout in seconds'
|
|
37
|
+
def purge_topology
|
|
38
|
+
Legion::CLI::AdminCommand.new([], options).purge_topology
|
|
39
|
+
end
|
|
26
40
|
end
|
|
27
41
|
end
|
|
28
42
|
end
|
data/lib/legion/version.rb
CHANGED