MCPcopy Create free account
hub / github.com/github/gh-aw / ExtractJSONCost

Function ExtractJSONCost

pkg/workflow/metrics.go:166–202  ·  view source on GitHub ↗

ExtractJSONCost extracts cost information from JSON data

(data map[string]any)

Source from the content-addressed store, hash-verified

164
165// ExtractJSONCost extracts cost information from JSON data
166func ExtractJSONCost(data map[string]any) float64 {
167 // Common cost field names
168 costFields := []string{"total_cost_usd", "cost", "price", "amount", "total_cost", "estimated_cost"}
169
170 // Prefer explicit total_cost_usd at top-level
171 if val, exists := data["total_cost_usd"]; exists {
172 if cost := typeutil.ConvertToFloat(val); cost > 0 {
173 if metricsLog.Enabled() {
174 metricsLog.Printf("Cost extracted: value=%.6f", cost)
175 }
176 return cost
177 }
178 }
179
180 for _, field := range costFields {
181 if val, exists := data[field]; exists {
182 if cost := typeutil.ConvertToFloat(val); cost > 0 {
183 return cost
184 }
185 }
186 }
187
188 // Check nested billing or pricing objects
189 if billing, exists := data["billing"]; exists {
190 if billingMap, ok := billing.(map[string]any); ok {
191 for _, field := range costFields {
192 if val, exists := billingMap[field]; exists {
193 if cost := typeutil.ConvertToFloat(val); cost > 0 {
194 return cost
195 }
196 }
197 }
198 }
199 }
200
201 return 0
202}
203
204// FinalizeToolMetricsOptions holds the options for FinalizeToolMetrics
205type FinalizeToolMetricsOptions struct {

Callers 3

TestExtractJSONCostFunction · 0.92
TestExtractJSONCostFunction · 0.85
ExtractJSONMetricsFunction · 0.85

Calls 3

ConvertToFloatFunction · 0.92
EnabledMethod · 0.45
PrintfMethod · 0.45

Tested by 2

TestExtractJSONCostFunction · 0.74
TestExtractJSONCostFunction · 0.68