lex-claude 0.3.0 → 0.3.2

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: 8689f00ee81cef79b268cbe7ee94cbc4e243488e34a68397af609543e6a34305
4
+ data.tar.gz: eb1c05e7db5f8e322d286b00263fd8aa402d0989860cb5c86443bbe0357671ec
5
5
  SHA512:
6
- metadata.gz: aa72850dab4628a3fcf6e6f6bd479ec5e3d4c9518710f05ab28f695c3f4b6834bb3615fbb79630b111d66317b13938a0145f281b93b6e0edab3a2e3e91f48c14
7
- data.tar.gz: a0a9cff50d0bbd456f5eb2fd0270132721ddfad15d60d140aa63277bbb80a604c27aafb1e56ddacfdd89e85d573aec8dc755fa58a8f7fcb907f672f543eb5cec
6
+ metadata.gz: 9f9a6640fc83eb4640df99eeb03d1581c1d6fab7addb59a23fe154804b760e4c41bdd51d994d433ed0c47ae0a9b7c53b0eee8e8bc90060cbef6aeabde641b1fa
7
+ data.tar.gz: 2f3ddb1f8d0d028e557f1b8037c0ba1754bfca699d276fab627c0a7de28e9d85ec5bd830eb484e64a8be72671b968dc8d9a8aecdfc52df50efa526a414209313
data/CHANGELOG.md CHANGED
@@ -1,5 +1,21 @@
1
1
  # Changelog
2
2
 
3
+ ## [0.3.2] - 2026-03-31
4
+
5
+ ### Added
6
+ - Specs for `context_management` beta support in `Runners::Messages#create` and `#create_stream`: body inclusion, beta header auto-injection (`context-management-2025-06-27`), and nil omission
7
+
8
+ ## [0.3.1] - 2026-03-31
9
+
10
+ ### Added
11
+ - Extended cache fields in `parse_usage`: `cache_ephemeral_1h_tokens`, `cache_ephemeral_5m_tokens`, `cache_deleted_tokens` from Anthropic's `cache_creation` nested object
12
+ - `count_tokens` now returns `:usage` key with standardized usage hash
13
+ - SSE `collect_usage` returns full 7-field usage hash including cache tokens from stream events
14
+
15
+ ### Changed
16
+ - `parse_usage` refactored to use `uval` helper for dual string/symbol key access
17
+ - SSE `collect_usage` uses `merge_usage` to generically accumulate all usage fields including nested `cache_creation`
18
+
3
19
  ## [0.3.0] - 2026-03-31
4
20
 
5
21
  ### 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.2'
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.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Esity