Only usage/attribution fields are exported: never prompts or reasoning text.
(change: &Change, hash: &Hash)
| 208 | |
| 209 | /// Only usage/attribution fields are exported: never prompts or reasoning text. |
| 210 | pub(crate) fn change_records(change: &Change, hash: &Hash) -> Vec<UsageRecord> { |
| 211 | change |
| 212 | .provenance() |
| 213 | .iter() |
| 214 | .enumerate() |
| 215 | .map(|(index, provenance)| { |
| 216 | let vendor = provenance.vendor.name().to_owned(); |
| 217 | let model = provenance.model.clone(); |
| 218 | let tool = provenance.tool.description(); |
| 219 | let turn = provenance |
| 220 | .metadata |
| 221 | .iter() |
| 222 | .find(|(key, _)| key == "turn_number") |
| 223 | .map(|(_, value)| value) |
| 224 | .filter(|value| !value.is_empty()); |
| 225 | let identity = if let Some(request) = provenance |
| 226 | .request_id |
| 227 | .as_deref() |
| 228 | .filter(|value| !value.is_empty()) |
| 229 | { |
| 230 | serde_json::to_vec(&("request", &vendor, request)) |
| 231 | } else if let (Some(session), Some(turn)) = ( |
| 232 | provenance |
| 233 | .session_id |
| 234 | .as_deref() |
| 235 | .filter(|value| !value.is_empty()), |
| 236 | turn, |
| 237 | ) { |
| 238 | serde_json::to_vec(&("session_turn", session, turn, &vendor, &model, &tool)) |
| 239 | } else { |
| 240 | // No identifiable shared generation: do not guess that otherwise |
| 241 | // identical records on distinct changes are the same paid request. |
| 242 | serde_json::to_vec(&("change_record", hash.to_base32(), index)) |
| 243 | } |
| 244 | .expect("usage identity serialization"); |
| 245 | let tokens = &provenance.tokens; |
| 246 | let cost = provenance.cost; |
| 247 | let zero_reported = provenance |
| 248 | .metadata |
| 249 | .iter() |
| 250 | .any(|(key, value)| key == "cost_reported" && value == "true"); |
| 251 | let cost_usd_micros = if cost.micro_usd > 0 || (cost.usd == 0.0 && zero_reported) { |
| 252 | Some(cost.micro_usd) |
| 253 | } else if cost.usd.is_finite() |
| 254 | && cost.usd > 0.0 |
| 255 | && cost.usd * 1_000_000.0 < u64::MAX as f64 |
| 256 | { |
| 257 | // Legacy records may predate the integer micro-dollar field. |
| 258 | Some((cost.usd * 1_000_000.0).round() as u64) |
| 259 | } else { |
| 260 | None |
| 261 | }; |
| 262 | UsageRecord { |
| 263 | id: Hash::of(&identity).to_base32(), |
| 264 | vendor, |
| 265 | model, |
| 266 | tool, |
| 267 | tokens: if [ |