@@ -5,6 +5,7 @@ import { dirname, join } from 'path'
55import {
66 type CacheDebugCorrelation ,
77} from '@codebuff/common/util/cache-debug'
8+ import type { CacheDebugUsageData } from '@codebuff/common/types/contracts/llm'
89import type { Logger } from '@codebuff/common/types/contracts/logger'
910import type { Message } from '@codebuff/common/types/messages/codebuff-message'
1011import type { ProviderMetadata } from '@codebuff/common/types/messages/provider-metadata'
@@ -50,6 +51,7 @@ export type CacheDebugSnapshot = {
5051 toolsHash ?: string
5152 preConversion : CacheDebugPreConversionSnapshot
5253 providerRequest ?: CacheDebugProviderRequestSnapshot
54+ usage ?: CacheDebugUsageData
5355}
5456
5557function getCacheDebugDir ( projectRoot : string ) {
@@ -241,6 +243,42 @@ export function createCacheDebugSnapshot(params: {
241243 return { snapshotId, filename, projectRoot }
242244}
243245
246+ export function enrichCacheDebugSnapshotWithUsage ( params : {
247+ correlation : CacheDebugCorrelation
248+ usage : CacheDebugUsageData
249+ logger : Logger
250+ } ) {
251+ const { correlation, usage, logger } = params
252+ try {
253+ const existing = loadSnapshot ( {
254+ projectRoot : correlation . projectRoot ,
255+ filename : correlation . filename ,
256+ } )
257+ if ( ! existing ) {
258+ logger . warn (
259+ `[Cache Debug] Could not find snapshot ${ correlation . filename } to enrich with usage` ,
260+ )
261+ return
262+ }
263+
264+ if ( existing . id !== correlation . snapshotId ) {
265+ logger . warn (
266+ `[Cache Debug] Snapshot ID mismatch while enriching ${ correlation . filename } with usage` ,
267+ )
268+ return
269+ }
270+
271+ const updated : CacheDebugSnapshot = {
272+ ...existing ,
273+ usage,
274+ }
275+
276+ writeSnapshot ( { snapshot : updated , logger } )
277+ } catch ( err ) {
278+ logger . warn ( { error : err } , '[Cache Debug] Failed to enrich snapshot with usage' )
279+ }
280+ }
281+
244282export function enrichCacheDebugSnapshotWithProviderRequest ( params : {
245283 correlation : CacheDebugCorrelation
246284 provider : string
0 commit comments