11import * as vscode from 'vscode'
22import * as fs from 'fs'
33import * as path from 'path'
4- import { TOKEN_COUNTS_CACHE } from '@/constants/state-keys'
54import { dictionary } from '@shared/constants/dictionary'
65import { Logger } from '@shared/utils/logger'
76import type { IWorkspaceProvider } from '../workspace-provider'
@@ -20,6 +19,8 @@ type TokenCountsCache = {
2019
2120const SHOW_COUNTING_NOTIFICATION_DELAY_MS = 3000
2221
22+ const TOKEN_CACHE_FILE_NAME = 'token-count-cache.json'
23+
2324export class TokenCalculator implements vscode . Disposable {
2425 private _file_token_counts : Map < string , number > = new Map ( )
2526 private _directory_token_counts : Map < string , number > = new Map ( )
@@ -35,9 +36,15 @@ export class TokenCalculator implements vscode.Disposable {
3536 this . _load_token_cache ( )
3637 }
3738
38- private _load_token_cache ( ) : void {
39- this . _token_cache =
40- this . _context . globalState . get < TokenCountsCache > ( TOKEN_COUNTS_CACHE ) ?? { }
39+ private async _load_token_cache ( ) : Promise < void > {
40+ try {
41+ const storage_path = this . _context . globalStorageUri . fsPath
42+ const cache_path = path . join ( storage_path , TOKEN_CACHE_FILE_NAME )
43+ const content = await fs . promises . readFile ( cache_path , 'utf8' )
44+ this . _token_cache = JSON . parse ( content )
45+ } catch ( error ) {
46+ this . _token_cache = { }
47+ }
4148 }
4249
4350 private _update_token_counts_cache ( ) : void {
@@ -48,9 +55,19 @@ export class TokenCalculator implements vscode.Disposable {
4855 }
4956
5057 this . _token_cache_update_timeout = setTimeout ( async ( ) => {
51- const current_global_cache =
52- this . _context . globalState . get < TokenCountsCache > ( TOKEN_COUNTS_CACHE ) ??
53- { }
58+ const storage_path = this . _context . globalStorageUri . fsPath
59+ const cache_path = path . join ( storage_path , TOKEN_CACHE_FILE_NAME )
60+ let current_global_cache : TokenCountsCache = { }
61+
62+ try {
63+ if ( ! fs . existsSync ( storage_path ) ) {
64+ await fs . promises . mkdir ( storage_path , { recursive : true } )
65+ }
66+ const content = await fs . promises . readFile ( cache_path , 'utf8' )
67+ current_global_cache = JSON . parse ( content )
68+ } catch {
69+ // Cache file might not exist yet or be corrupt
70+ }
5471
5572 for ( const root of this . _provider . getWorkspaceRoots ( ) ) {
5673 if ( this . _token_cache [ root ] ) {
@@ -66,16 +83,24 @@ export class TokenCalculator implements vscode.Disposable {
6683 }
6784 }
6885
69- await this . _context . globalState . update (
70- TOKEN_COUNTS_CACHE ,
71- current_global_cache
72- )
86+ try {
87+ await fs . promises . writeFile (
88+ cache_path ,
89+ JSON . stringify ( current_global_cache )
90+ )
7391
74- Logger . info ( {
75- function_name : '_update_token_counts_cache' ,
76- message : 'Token counts cache updated'
77- } )
78- this . _has_token_counts_cache_updated_once = true
92+ Logger . info ( {
93+ function_name : '_update_token_counts_cache' ,
94+ message : 'Token counts cache updated'
95+ } )
96+ this . _has_token_counts_cache_updated_once = true
97+ } catch ( error ) {
98+ Logger . error ( {
99+ function_name : '_update_token_counts_cache' ,
100+ message : 'Error writing token cache' ,
101+ data : error
102+ } )
103+ }
79104 } , 10000 )
80105 }
81106
0 commit comments