@@ -6,11 +6,17 @@ import { Logger } from '@shared/utils/logger'
66import type { IWorkspaceProvider } from '../workspace-provider'
77import { compact_file } from '../../../utils/compact-file/compact-file'
88
9+ type TokenData = [ number , number , number ]
10+
11+ type TokenCacheNode = {
12+ [ key : string ] : TokenCacheNode | TokenData
13+ }
14+
915type TokenCountsCache = {
1016 [ workspace_root : string ] : {
1117 modified_at : number
1218 files : {
13- [ file_path : string ] : [ number , number , number ]
19+ [ file_path : string ] : TokenData
1420 }
1521 }
1622}
@@ -44,7 +50,14 @@ export class TokenCalculator implements vscode.Disposable {
4450 const storage_path = this . _context . globalStorageUri . fsPath
4551 const cache_path = path . join ( storage_path , TOKEN_CACHE_FILE_NAME )
4652 const content = await fs . promises . readFile ( cache_path , 'utf8' )
47- this . _token_cache = JSON . parse ( content )
53+ const raw_cache = JSON . parse ( content )
54+ this . _token_cache = { }
55+ for ( const root in raw_cache ) {
56+ this . _token_cache [ root ] = {
57+ modified_at : raw_cache [ root ] . modified_at ,
58+ files : this . _flatten_tree ( raw_cache [ root ] . files || { } )
59+ }
60+ }
4861 } catch ( error ) {
4962 this . _token_cache = { }
5063 }
@@ -60,21 +73,33 @@ export class TokenCalculator implements vscode.Disposable {
6073 this . _token_cache_update_timeout = setTimeout ( async ( ) => {
6174 const storage_path = this . _context . globalStorageUri . fsPath
6275 const cache_path = path . join ( storage_path , TOKEN_CACHE_FILE_NAME )
63- let current_global_cache : TokenCountsCache = { }
76+ const current_global_cache : TokenCountsCache = { }
6477
6578 try {
6679 if ( ! fs . existsSync ( storage_path ) ) {
6780 await fs . promises . mkdir ( storage_path , { recursive : true } )
6881 }
6982 const content = await fs . promises . readFile ( cache_path , 'utf8' )
70- current_global_cache = JSON . parse ( content )
83+ const raw_cache = JSON . parse ( content )
84+ for ( const root in raw_cache ) {
85+ current_global_cache [ root ] = {
86+ modified_at : raw_cache [ root ] . modified_at ,
87+ files : this . _flatten_tree ( raw_cache [ root ] . files || { } )
88+ }
89+ }
7190 } catch {
7291 // Cache file might not exist yet or be corrupt
7392 }
7493
7594 for ( const root of this . _provider . get_workspace_roots ( ) ) {
7695 if ( this . _session_cache [ root ] ) {
77- current_global_cache [ root ] = this . _session_cache [ root ]
96+ current_global_cache [ root ] = {
97+ modified_at : this . _session_cache [ root ] . modified_at ,
98+ files : {
99+ ...( current_global_cache [ root ] ?. files || { } ) ,
100+ ...this . _session_cache [ root ] . files
101+ }
102+ }
78103 }
79104 }
80105
@@ -86,11 +111,16 @@ export class TokenCalculator implements vscode.Disposable {
86111 }
87112 }
88113
114+ const cache_to_write : any = { }
115+ for ( const root in current_global_cache ) {
116+ cache_to_write [ root ] = {
117+ modified_at : current_global_cache [ root ] . modified_at ,
118+ files : this . _build_tree ( current_global_cache [ root ] . files )
119+ }
120+ }
121+
89122 try {
90- await fs . promises . writeFile (
91- cache_path ,
92- JSON . stringify ( current_global_cache )
93- )
123+ await fs . promises . writeFile ( cache_path , JSON . stringify ( cache_to_write ) )
94124
95125 Logger . info ( {
96126 function_name : '_update_token_counts_cache' ,
@@ -594,4 +624,51 @@ export class TokenCalculator implements vscode.Disposable {
594624 clearTimeout ( this . _token_cache_update_timeout )
595625 }
596626 }
627+
628+ private _flatten_tree (
629+ node : TokenCacheNode ,
630+ prefix : string = ''
631+ ) : { [ file_path : string ] : TokenData } {
632+ const result : { [ file_path : string ] : TokenData } = { }
633+
634+ for ( const key in node ) {
635+ const value = node [ key ]
636+ const new_path = prefix ? `${ prefix } /${ key } ` : key
637+
638+ if ( Array . isArray ( value ) ) {
639+ result [ new_path ] = value as TokenData
640+ } else {
641+ const flat_children = this . _flatten_tree (
642+ value as TokenCacheNode ,
643+ new_path
644+ )
645+ Object . assign ( result , flat_children )
646+ }
647+ }
648+
649+ return result
650+ }
651+
652+ private _build_tree ( files : {
653+ [ file_path : string ] : TokenData
654+ } ) : TokenCacheNode {
655+ const root : TokenCacheNode = { }
656+
657+ for ( const [ file_path , data ] of Object . entries ( files ) ) {
658+ const parts = file_path . split ( '/' )
659+ let current = root
660+
661+ for ( let i = 0 ; i < parts . length - 1 ; i ++ ) {
662+ const part = parts [ i ]
663+ if ( ! current [ part ] || Array . isArray ( current [ part ] ) ) {
664+ current [ part ] = { }
665+ }
666+ current = current [ part ] as TokenCacheNode
667+ }
668+
669+ current [ parts [ parts . length - 1 ] ] = data
670+ }
671+
672+ return root
673+ }
597674}
0 commit comments