Add `add`'s numeric counters field-wise into `existing` (both are usage objects). Used when several flushes land on the same assistant message (e.g. an aborted turn with no reply of its own).
(existing: &mut Value, add: &Value)
| 933 | /// objects). Used when several flushes land on the same assistant message |
| 934 | /// (e.g. an aborted turn with no reply of its own). |
| 935 | pub(crate) fn merge_usage_counters(existing: &mut Value, add: &Value) { |
| 936 | let (Some(map), Some(add_map)) = (existing.as_object_mut(), add.as_object()) else { |
| 937 | return; |
| 938 | }; |
| 939 | for (key, value) in add_map { |
| 940 | if let Some(count) = value.as_i64() { |
| 941 | let current = map.get(key).and_then(Value::as_i64).unwrap_or(0); |
| 942 | map.insert(key.clone(), Value::from(current.saturating_add(count))); |
| 943 | } |
| 944 | } |
| 945 | } |
| 946 | |
| 947 | /// Attach the finished turn's summed usage to the most recent assistant |
| 948 | /// message of the batch (the reply the turn's `token_count` events report |
no test coverage detected