@@ -65,23 +65,66 @@ export class ContextProxy {
6565 this . _isInitialized = false
6666 }
6767
68+ /**
69+ * Helper method to get values from either VSCode context or platform services
70+ */
71+ private getFromStorage < T > ( key : string , storageType : "globalState" | "secrets" = "globalState" ) : T | undefined {
72+ if ( isVsCodeContext ( ) && this . originalContext ) {
73+ if ( storageType === "secrets" ) {
74+ return this . originalContext . secrets . get ( key )
75+ }
76+ return this . originalContext . globalState . get ( key )
77+ } else if ( this . platformServices ) {
78+ if ( storageType === "secrets" ) {
79+ // In CLI mode, secrets are stored as prefixed config keys
80+ return this . platformServices . configuration . get ( `secret:${ key } ` ) as T
81+ }
82+ return this . platformServices . configuration . get ( key ) as T
83+ }
84+ return undefined
85+ }
86+
87+ /**
88+ * Helper method to set values to either VSCode context or platform services
89+ */
90+ private async setToStorage < T > (
91+ key : string ,
92+ value : T ,
93+ storageType : "globalState" | "secrets" = "globalState" ,
94+ ) : Promise < void > {
95+ if ( isVsCodeContext ( ) && this . originalContext ) {
96+ if ( storageType === "secrets" ) {
97+ return value === undefined
98+ ? this . originalContext . secrets . delete ( key )
99+ : this . originalContext . secrets . store ( key , value )
100+ }
101+ return this . originalContext . globalState . update ( key , value )
102+ } else if ( this . platformServices ) {
103+ if ( storageType === "secrets" ) {
104+ // In CLI mode, store secrets as regular config (not ideal but functional)
105+ // TODO: Implement proper secret storage for CLI
106+ return this . platformServices . configuration . set ( `secret:${ key } ` , value )
107+ }
108+ return this . platformServices . configuration . set ( key , value )
109+ }
110+ }
111+
68112 public get isInitialized ( ) {
69113 return this . _isInitialized
70114 }
71115
72116 public async initialize ( ) {
73117 for ( const key of GLOBAL_STATE_KEYS ) {
74118 try {
75- // Revert to original assignment
76- this . stateCache [ key ] = this . originalContext . globalState . get ( key )
119+ this . stateCache [ key ] = this . getFromStorage ( key )
77120 } catch ( error ) {
78121 logger . error ( `Error loading global ${ key } : ${ error instanceof Error ? error . message : String ( error ) } ` )
79122 }
80123 }
81124
82125 const promises = SECRET_STATE_KEYS . map ( async ( key ) => {
83126 try {
84- this . secretCache [ key ] = await this . originalContext . secrets . get ( key )
127+ this . secretCache [ key ] = await this . getFromStorage ( key , "secrets" )
85128 } catch ( error ) {
86129 logger . error ( `Error loading secret ${ key } : ${ error instanceof Error ? error . message : String ( error ) } ` )
87130 }
@@ -125,14 +168,7 @@ export class ContextProxy {
125168 getGlobalState < K extends GlobalStateKey > ( key : K , defaultValue : GlobalState [ K ] ) : GlobalState [ K ]
126169 getGlobalState < K extends GlobalStateKey > ( key : K , defaultValue ?: GlobalState [ K ] ) : GlobalState [ K ] {
127170 if ( isPassThroughStateKey ( key ) ) {
128- let value : GlobalState [ K ] | undefined
129-
130- if ( isVsCodeContext ( ) && this . originalContext ) {
131- value = this . originalContext . globalState . get ( key )
132- } else if ( this . platformServices ) {
133- // CLI mode - use platform services configuration
134- value = this . platformServices . configuration . get ( key ) as GlobalState [ K ]
135- }
171+ const value = this . getFromStorage < GlobalState [ K ] > ( key )
136172 return value === undefined || value === null ? defaultValue : value
137173 }
138174
@@ -142,20 +178,11 @@ export class ContextProxy {
142178
143179 async updateGlobalState < K extends GlobalStateKey > ( key : K , value : GlobalState [ K ] ) {
144180 if ( isPassThroughStateKey ( key ) ) {
145- if ( isVsCodeContext ( ) && this . originalContext ) {
146- return this . originalContext . globalState . update ( key , value )
147- } else if ( this . platformServices ) {
148- return this . platformServices . configuration . set ( key , value )
149- }
181+ return this . setToStorage ( key , value )
150182 }
151183
152184 this . stateCache [ key ] = value
153-
154- if ( isVsCodeContext ( ) && this . originalContext ) {
155- return this . originalContext . globalState . update ( key , value )
156- } else if ( this . platformServices ) {
157- return this . platformServices . configuration . set ( key , value )
158- }
185+ return this . setToStorage ( key , value )
159186 }
160187
161188 private getAllGlobalState ( ) : GlobalState {
@@ -176,15 +203,7 @@ export class ContextProxy {
176203 this . secretCache [ key ] = value
177204
178205 // Write directly to context or platform storage
179- if ( isVsCodeContext ( ) && this . originalContext ) {
180- return value === undefined
181- ? this . originalContext . secrets . delete ( key )
182- : this . originalContext . secrets . store ( key , value )
183- } else if ( this . platformServices ) {
184- // In CLI mode, store secrets as regular config (not ideal but functional)
185- // TODO: Implement proper secret storage for CLI
186- return this . platformServices . configuration . set ( `secret:${ key } ` , value )
187- }
206+ return this . setToStorage ( key , value , "secrets" )
188207 }
189208
190209 private getAllSecretState ( ) : SecretState {
@@ -315,8 +334,8 @@ export class ContextProxy {
315334 this . secretCache = { }
316335
317336 await Promise . all ( [
318- ...GLOBAL_STATE_KEYS . map ( ( key ) => this . originalContext . globalState . update ( key , undefined ) ) ,
319- ...SECRET_STATE_KEYS . map ( ( key ) => this . originalContext . secrets . delete ( key ) ) ,
337+ ...GLOBAL_STATE_KEYS . map ( ( key ) => this . setToStorage ( key , undefined ) ) ,
338+ ...SECRET_STATE_KEYS . map ( ( key ) => this . setToStorage ( key , undefined , "secrets" ) ) ,
320339 ] )
321340
322341 await this . initialize ( )
0 commit comments