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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5af5e876210c79ae9fcc876fad3b8086a605f3a841c450793b4759711c5b0101
4
- data.tar.gz: 43d1c51c4a52121654e1a1e426f5bbe9e6e1771a5a95a095eb12e87474632eec
3
+ metadata.gz: 537bbc9c431e5e8b9fdaf96145ea124a0c4b8e84b4094673cbc4f6b07d4edb21
4
+ data.tar.gz: da76364c12123a27f644f4fdef175c14796b2542800a93b054b26cb5f27a5e79
5
5
  SHA512:
6
- metadata.gz: af956fad95ae9de341f700fa1e03997bed9b889983dc69b96322ba72cb7871f584224b4d00112e17b2e580a9f37e1afff3fa96e9b5bb5e83a38cf487fad26b77
7
- data.tar.gz: 6545603e7df04444096ae7763fcfde3ee4b970603554388ab04be50a088bd36ac1f77c0fc5ce02f04eeaf5396bb5620d38fc6416f5125b900225a3f79ecc773a
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
@@ -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
- id: tc.respond_to?(:id) ? tc.id : nil,
322
- name: tc.respond_to?(:name) ? tc.name : tc.to_s,
323
- arguments: tc.respond_to?(:arguments) ? tc.arguments : {}
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
- content: full_text,
334
- model: pipeline_response.routing&.dig(:model),
335
- input_tokens: tokens.respond_to?(:input_tokens) ? tokens.input_tokens : nil,
336
- output_tokens: tokens.respond_to?(:output_tokens) ? tokens.output_tokens : nil
337
- })}\n\n"
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, 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'
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) do |http|
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
- tool_calls.each do |tc|
172
- tc = tc.transform_keys(&:to_sym) if tc.respond_to?(:transform_keys)
173
- tc_obj = build_tool_call_object(tc)
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
- @on_tool_call&.call(tc_obj)
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
- result_text = run_tool(tc)
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
- result_obj = build_tool_result_object(result_text)
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
- def build_tool_result_object(text)
199
- Struct.new(:content).new(content: text.to_s)
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Legion
4
- VERSION = '1.7.29'
4
+ VERSION = '1.7.30'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: legionio
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.7.29
4
+ version: 1.7.30
5
5
  platform: ruby
6
6
  authors:
7
7
  - Esity