lex-claude 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: 0b422c38d51391b457e88f4760b941afe22a835be9ba5405812a89024e082bb4
4
- data.tar.gz: 23c95a1d66de519a7f24d9bdf86d522d13cc53b47732bdce3709dcee09df20bd
3
+ metadata.gz: 513c8f540e4cdda4b2843e24546337c6f1ae55204b86b7393ee5f6dfa821b00a
4
+ data.tar.gz: 83adddcbdaf5b8f270c8cc9405f34dd9ff9bd0524e600ee5d17aa83c6fcb1490
5
5
  SHA512:
6
- metadata.gz: aa72850dab4628a3fcf6e6f6bd479ec5e3d4c9518710f05ab28f695c3f4b6834bb3615fbb79630b111d66317b13938a0145f281b93b6e0edab3a2e3e91f48c14
7
- data.tar.gz: a0a9cff50d0bbd456f5eb2fd0270132721ddfad15d60d140aa63277bbb80a604c27aafb1e56ddacfdd89e85d573aec8dc755fa58a8f7fcb907f672f543eb5cec
6
+ metadata.gz: ae5e070c06fa3121427b81d2bac44adae31690aa3592a4289334eef6f5ebccfb7d885cc8c4eee436e99947f461a4b2f29d12c546b7fc1077c00f9723f9215608
7
+ data.tar.gz: 6f18422ad53790e20cf2fda651881fcac2679bbcff9da38199cc817cf38ca715d1d660138edab3d899a7996841d98a38ecdd0368aac029bbd861ee0a4a3e5432
data/CHANGELOG.md CHANGED
@@ -1,5 +1,16 @@
1
1
  # Changelog
2
2
 
3
+ ## [0.3.1] - 2026-03-31
4
+
5
+ ### Added
6
+ - Extended cache fields in `parse_usage`: `cache_ephemeral_1h_tokens`, `cache_ephemeral_5m_tokens`, `cache_deleted_tokens` from Anthropic's `cache_creation` nested object
7
+ - `count_tokens` now returns `:usage` key with standardized usage hash
8
+ - SSE `collect_usage` returns full 7-field usage hash including cache tokens from stream events
9
+
10
+ ### Changed
11
+ - `parse_usage` refactored to use `uval` helper for dual string/symbol key access
12
+ - SSE `collect_usage` uses `merge_usage` to generically accumulate all usage fields including nested `cache_creation`
13
+
3
14
  ## [0.3.0] - 2026-03-31
4
15
 
5
16
  ### Added
@@ -47,13 +47,23 @@ module Legion
47
47
 
48
48
  def parse_usage(body)
49
49
  usage = body.is_a?(Hash) ? (body[:usage] || body['usage'] || {}) : {} # rubocop:disable Legion/Framework/ApiStringKeys
50
+ cache_creation = usage.is_a?(Hash) ? (usage[:cache_creation] || usage['cache_creation'] || {}) : {}
50
51
  {
51
- input_tokens: (usage[:input_tokens] || usage['input_tokens'] || 0).to_i,
52
- output_tokens: (usage[:output_tokens] || usage['output_tokens'] || 0).to_i,
53
- cache_read_tokens: (usage[:cache_read_input_tokens] || usage['cache_read_input_tokens'] || 0).to_i,
54
- cache_write_tokens: (usage[:cache_creation_input_tokens] || usage['cache_creation_input_tokens'] || 0).to_i
52
+ input_tokens: uval(usage, :input_tokens),
53
+ output_tokens: uval(usage, :output_tokens),
54
+ cache_read_tokens: uval(usage, :cache_read_input_tokens),
55
+ cache_write_tokens: uval(usage, :cache_creation_input_tokens),
56
+ cache_ephemeral_1h_tokens: uval(cache_creation, :ephemeral_1h_input_tokens),
57
+ cache_ephemeral_5m_tokens: uval(cache_creation, :ephemeral_5m_input_tokens),
58
+ cache_deleted_tokens: uval(usage, :cache_deleted_input_tokens)
55
59
  }
56
60
  end
61
+
62
+ def uval(hash, key)
63
+ return 0 unless hash.is_a?(Hash)
64
+
65
+ (hash[key] || hash[key.to_s] || 0).to_i
66
+ end
57
67
  end
58
68
  end
59
69
  end
@@ -45,22 +45,38 @@ module Legion
45
45
  end
46
46
 
47
47
  def collect_usage(events)
48
- input_tokens = 0
49
- output_tokens = 0
48
+ totals = Hash.new(0)
50
49
 
51
50
  events.each do |e|
52
51
  case e[:event]
53
52
  when 'message_start'
54
- usage = e[:data].dig('message', 'usage') || {}
55
- input_tokens += usage.fetch('input_tokens', 0).to_i
56
- output_tokens += usage.fetch('output_tokens', 0).to_i
53
+ merge_usage(totals, e[:data].dig('message', 'usage'))
57
54
  when 'message_delta'
58
- usage = e[:data].fetch('usage', {})
59
- output_tokens += usage.fetch('output_tokens', 0).to_i
55
+ merge_usage(totals, e[:data]['usage'])
60
56
  end
61
57
  end
62
58
 
63
- { input_tokens: input_tokens, output_tokens: output_tokens }
59
+ {
60
+ input_tokens: totals['input_tokens'],
61
+ output_tokens: totals['output_tokens'],
62
+ cache_read_tokens: totals['cache_read_input_tokens'],
63
+ cache_write_tokens: totals['cache_creation_input_tokens'],
64
+ cache_ephemeral_1h_tokens: totals['ephemeral_1h_input_tokens'],
65
+ cache_ephemeral_5m_tokens: totals['ephemeral_5m_input_tokens'],
66
+ cache_deleted_tokens: totals['cache_deleted_input_tokens']
67
+ }
68
+ end
69
+
70
+ def merge_usage(totals, usage)
71
+ return unless usage.is_a?(Hash)
72
+
73
+ usage.each do |key, val|
74
+ if val.is_a?(Hash)
75
+ val.each { |k, v| totals[k.to_s] += v.to_i }
76
+ else
77
+ totals[key.to_s] += val.to_i
78
+ end
79
+ end
64
80
  end
65
81
  end
66
82
  end
@@ -61,7 +61,9 @@ module Legion
61
61
  resolved_betas << :interleaved_thinking if thinking && !resolved_betas.include?(:interleaved_thinking)
62
62
 
63
63
  response = client(api_key: api_key, betas: resolved_betas).post('/v1/messages/count_tokens', body)
64
- Helpers::Response.handle_response(response)
64
+ result = Helpers::Response.handle_response(response)
65
+ result[:usage] = Helpers::Response.parse_usage(response.body) if response.body.is_a?(Hash)
66
+ result
65
67
  end
66
68
 
67
69
  private
@@ -3,7 +3,7 @@
3
3
  module Legion
4
4
  module Extensions
5
5
  module Claude
6
- VERSION = '0.3.0'
6
+ VERSION = '0.3.1'
7
7
  end
8
8
  end
9
9
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lex-claude
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
  - Esity