activeagents-telemetry-ruby_llm 0.3.0 → 0.3.1

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: 5abf7d85a5deec9d008170f9c9a3e50d619024b7bad8c1f01eea3eda3d3befc6
4
- data.tar.gz: ea10cb9f3cb45700fe8ad0642c72eb3c6e01576c1e4353d274998ead5f3f7243
3
+ metadata.gz: 255ca4c32e9f775c8b2e45ab431ae81ee410fa3002d66dce026ba06d948da4b6
4
+ data.tar.gz: 1afbdf608524f69045341e10cd7a6f722854cc49977e27ff511cafcc8368fa72
5
5
  SHA512:
6
- metadata.gz: 67e5b7d06a3de8510dead200b6ec9a4da44172d24b46c3ea02646cfd4fad1fc3c5acb960798e17a5957f61d8dcd4bd7efc392ae08f8e46ef19b084014e357073
7
- data.tar.gz: 16dde44b30870de36415b0958b85eabff49caf639238baf140107e6b2a986effe21bf0eea529c78b5810d90816098cac8c35f09800304c3c5cddd3c7ab6b3c22
6
+ metadata.gz: 5b316442b6a5bb9044df92b691d6d4dee824d12522c45f4367d252cf7041aeee2be2060ce3a604f9b0e36b814c04e78ef5280646ad67ae7f893c29f0a1799c2b
7
+ data.tar.gz: 2fe33cb43e2a35659c2112decb662199b461f756e8ef63560e4e7b63151fde0fdcfa1a4f042c6f0419bead7e41787ec43ba51fdbcee58bc4cd1550014a3f56b5
@@ -3,7 +3,7 @@
3
3
  module ActiveAgents
4
4
  module Telemetry
5
5
  module RubyLLM
6
- VERSION = "0.3.0"
6
+ VERSION = "0.3.1"
7
7
  end
8
8
  end
9
9
  end
@@ -43,6 +43,9 @@ module ActiveAgents
43
43
 
44
44
  DEFAULT_AGENT = { name: "RubyLLM::Chat", action: "chat" }.freeze
45
45
 
46
+ # Span token keys and the reader that answers each on a RubyLLM 2.x +Tokens+.
47
+ TOKEN_READERS = { "input" => :input, "output" => :output, "thinking" => :thinking }.freeze
48
+
46
49
  State = Struct.new(:depth, :started_at, :tool_spans, :rounds, :tokens, :chat_key, :agent)
47
50
 
48
51
  class << self
@@ -306,22 +309,39 @@ module ActiveAgents
306
309
  text.to_s[0, CONTENT_LIMIT]
307
310
  end
308
311
 
312
+ # Tokens for one round. RubyLLM 2.x puts the round's +Tokens+ on the
313
+ # payload and moves the per-message readers to +message.tokens+; 1.x
314
+ # has +input_tokens+ and friends on each assistant message and nothing
315
+ # on the payload. The payload is read first, then the messages the
316
+ # round added, with either generation's readers.
309
317
  def token_totals(payload)
318
+ tokens = payload_tokens(payload[:tokens]) || message_tokens(payload)
319
+ tokens["total"] = tokens.values.sum
320
+ tokens
321
+ end
322
+
323
+ def payload_tokens(tokens)
324
+ return unless TOKEN_READERS.values.all? { |reader| tokens.respond_to?(reader) }
325
+
326
+ TOKEN_READERS.to_h { |key, reader| [ key, tokens.public_send(reader).to_i ] }
327
+ end
328
+
329
+ def message_tokens(payload)
310
330
  initial_count = Array(payload[:input_messages]).size
311
331
  new_messages = Array(payload[:messages_after])[initial_count..] || []
312
332
  assistant_messages = new_messages.select { |message| message.respond_to?(:role) && message.role.to_s == "assistant" }
313
333
 
314
- tokens = {
315
- "input" => sum_tokens(assistant_messages, :input_tokens),
316
- "output" => sum_tokens(assistant_messages, :output_tokens),
317
- "thinking" => sum_tokens(assistant_messages, :thinking_tokens)
318
- }
319
- tokens["total"] = tokens.values.sum
320
- tokens
334
+ TOKEN_READERS.to_h { |key, reader| [ key, assistant_messages.sum { |message| message_token_count(message, key, reader) } ] }
321
335
  end
322
336
 
323
- def sum_tokens(messages, method_name)
324
- messages.sum { |message| message.respond_to?(method_name) ? message.public_send(method_name).to_i : 0 }
337
+ # A 1.x message answers +input_tokens+; a 2.x message answers
338
+ # +tokens.input+.
339
+ def message_token_count(message, key, reader)
340
+ legacy_reader = :"#{key}_tokens"
341
+ return message.public_send(legacy_reader).to_i if message.respond_to?(legacy_reader)
342
+
343
+ tokens = message.tokens if message.respond_to?(:tokens)
344
+ tokens.respond_to?(reader) ? tokens.public_send(reader).to_i : 0
325
345
  end
326
346
  end
327
347
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activeagents-telemetry-ruby_llm
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - ActiveAgents
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2026-09-13 00:00:00.000000000 Z
11
+ date: 2026-09-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activeagents-telemetry