active_harness 0.2.12 → 0.2.13

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: f4f069d1d894324475b0a5395d5728fa77626698255bc3400428ef66a2228ac4
4
- data.tar.gz: 1cbfee779b26e122d0e01e3fa1e4a80907398eec5f0895b28bc1dcb19484d8a4
3
+ metadata.gz: 0c5339e68dc9f7de87fc67a4e986cd1858e86f52e95ee277ee0f12563eedf428
4
+ data.tar.gz: d563d7088e6b29c2119160a9eb0e0a39eb550d12e1bf828937a7d20fddcf7ad4
5
5
  SHA512:
6
- metadata.gz: c2293baad25b739468f7fb3306583f2e475409edc7b1533a03e83da9e28776ba17a27ae6a9aebf47fb18d4a1953957240d2615a277443e39781876a685d7da1e
7
- data.tar.gz: 6e78901ee75204d853b53df08baef9215f7f225f15065da771262ee084af17243a796ab132526701b016e4d0066762338e01fd01fd36f5e316e1045599cc8c3b
6
+ metadata.gz: 9d710fedd64ae1bd1ee40616aacb884e698491ec223895651c1ad28a307121575f5078df54074e3218ade227cf376f20a881af15ecef640e63f74887cd5a9115
7
+ data.tar.gz: '075778aea6ae0b53624b18fd64835cdaaec6e0381ff7616faab86c2b9813d5ae80830bdcf59ce71b255a13930ad6d35f99b1953bde11e58c7a8bda03c36732df'
@@ -0,0 +1,28 @@
1
+ module ActiveHarness
2
+ class Agent
3
+ private
4
+
5
+ # Calculates the monetary cost of a single request based on token usage
6
+ # and pricing data from ActiveHarness::Costs.
7
+ #
8
+ # Returns a hash { input_cost:, output_cost:, total_cost: } in USD,
9
+ # or nil if usage is absent or the model is not found in the pricing registry.
10
+ def calculate_cost(model_id, usage)
11
+ return nil if model_id.nil? || usage.nil?
12
+
13
+ pricing = ActiveHarness::Costs.find(model_id.to_s)
14
+ return nil unless pricing&.input_per_million && pricing&.output_per_million
15
+
16
+ input_cost = (usage[:input_tokens].to_f / 1_000_000) * pricing.input_per_million
17
+ output_cost = (usage[:output_tokens].to_f / 1_000_000) * pricing.output_per_million
18
+
19
+ {
20
+ input_cost: input_cost.round(8),
21
+ output_cost: output_cost.round(8),
22
+ total_cost: (input_cost + output_cost).round(8)
23
+ }
24
+ rescue StandardError
25
+ nil
26
+ end
27
+ end
28
+ end
@@ -113,6 +113,7 @@ module ActiveHarness
113
113
  def build_result(response, entry, attempts, elapsed)
114
114
  raw = response[:content]
115
115
  parsed = parse_output(raw)
116
+ usage = response[:usage]
116
117
 
117
118
  Result.new(
118
119
  input: @input,
@@ -125,7 +126,8 @@ module ActiveHarness
125
126
  model_list: model_list,
126
127
  attempts: attempts,
127
128
  execution_time: elapsed,
128
- usage: response[:usage]
129
+ usage: usage,
130
+ cost: calculate_cost(entry[:model], usage)
129
131
  )
130
132
  end
131
133
 
@@ -152,4 +154,5 @@ require_relative "agent/models"
152
154
  require_relative "agent/providers"
153
155
  require_relative "agent/output_parser"
154
156
  require_relative "agent/ruby_llm_backend"
157
+ require_relative "agent/cost"
155
158
 
@@ -183,10 +183,21 @@ module ActiveHarness
183
183
  end
184
184
 
185
185
  def registry
186
- @registry ||= begin
187
- source = File.exist?(cache_file) ? cache_file : BUNDLED_DATA_FILE
188
- JSON.parse(File.read(source), symbolize_names: true)
186
+ @registry ||= load_registry
187
+ end
188
+
189
+ def load_registry
190
+ if File.exist?(cache_file)
191
+ begin
192
+ data = JSON.parse(File.read(cache_file), symbolize_names: true)
193
+ return data if data.is_a?(Array)
194
+ rescue JSON::ParserError
195
+ # Cache file corrupted — fall through to bundled data
196
+ end
189
197
  end
198
+ JSON.parse(File.read(BUNDLED_DATA_FILE), symbolize_names: true)
199
+ rescue JSON::ParserError, Errno::ENOENT
200
+ []
190
201
  end
191
202
 
192
203
  def fetch_models_dev
@@ -6,5 +6,6 @@ module ActiveHarness
6
6
  # output — raw string from the provider
7
7
  # parsed — for format :json: a Ruby Hash/Array; for format :text: same as output
8
8
  # usage — token counts: { input_tokens:, output_tokens:, total_tokens: } or nil for streaming
9
- Result = Struct.new(:input, :output, :parsed, :system_prompt, :provider, :model, :temperature, :model_list, :attempts, :execution_time, :usage, keyword_init: true)
9
+ # cost — { input_cost:, output_cost:, total_cost: } in USD, or nil if pricing unavailable
10
+ Result = Struct.new(:input, :output, :parsed, :system_prompt, :provider, :model, :temperature, :model_list, :attempts, :execution_time, :usage, :cost, keyword_init: true)
10
11
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: active_harness
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.12
4
+ version: 0.2.13
5
5
  platform: ruby
6
6
  authors:
7
7
  - the-teacher
@@ -33,6 +33,7 @@ extra_rdoc_files: []
33
33
  files:
34
34
  - lib/active_harness.rb
35
35
  - lib/active_harness/agent.rb
36
+ - lib/active_harness/agent/cost.rb
36
37
  - lib/active_harness/agent/hooks.rb
37
38
  - lib/active_harness/agent/models.rb
38
39
  - lib/active_harness/agent/output_parser.rb