ExtractJSONCost extracts cost information from JSON data
(data map[string]any)
| 164 | |
| 165 | // ExtractJSONCost extracts cost information from JSON data |
| 166 | func 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 |
| 205 | type FinalizeToolMetricsOptions struct { |