@@ -33,15 +33,17 @@ export class AuthService extends EventEmitter<AuthServiceEvents> {
3333 private context : vscode . ExtensionContext
3434 private timer : RefreshTimer
3535 private state : AuthState = "initializing"
36+ private log : ( ...args : unknown [ ] ) => void
3637
3738 private credentials : AuthCredentials | null = null
3839 private sessionToken : string | null = null
3940 private userInfo : CloudUserInfo | null = null
4041
41- constructor ( context : vscode . ExtensionContext ) {
42+ constructor ( context : vscode . ExtensionContext , log ?: ( ... args : unknown [ ] ) => void ) {
4243 super ( )
4344
4445 this . context = context
46+ this . log = log || console . log
4547
4648 this . timer = new RefreshTimer ( {
4749 callback : async ( ) => {
@@ -72,7 +74,7 @@ export class AuthService extends EventEmitter<AuthServiceEvents> {
7274 }
7375 }
7476 } catch ( error ) {
75- console . error ( "[auth] Error handling credentials change:" , error )
77+ this . log ( "[auth] Error handling credentials change:" , error )
7678 }
7779 }
7880
@@ -88,7 +90,7 @@ export class AuthService extends EventEmitter<AuthServiceEvents> {
8890
8991 this . emit ( "logged-out" , { previousState } )
9092
91- console . log ( "[auth] Transitioned to logged-out state" )
93+ this . log ( "[auth] Transitioned to logged-out state" )
9294 }
9395
9496 private transitionToInactiveSession ( credentials : AuthCredentials ) : void {
@@ -104,7 +106,7 @@ export class AuthService extends EventEmitter<AuthServiceEvents> {
104106
105107 this . timer . start ( )
106108
107- console . log ( "[auth] Transitioned to inactive-session state" )
109+ this . log ( "[auth] Transitioned to inactive-session state" )
108110 }
109111
110112 /**
@@ -115,7 +117,7 @@ export class AuthService extends EventEmitter<AuthServiceEvents> {
115117 */
116118 public async initialize ( ) : Promise < void > {
117119 if ( this . state !== "initializing" ) {
118- console . log ( "[auth] initialize() called after already initialized" )
120+ this . log ( "[auth] initialize() called after already initialized" )
119121 return
120122 }
121123
@@ -143,9 +145,9 @@ export class AuthService extends EventEmitter<AuthServiceEvents> {
143145 return authCredentialsSchema . parse ( parsedJson )
144146 } catch ( error ) {
145147 if ( error instanceof z . ZodError ) {
146- console . error ( "[auth] Invalid credentials format:" , error . errors )
148+ this . log ( "[auth] Invalid credentials format:" , error . errors )
147149 } else {
148- console . error ( "[auth] Failed to parse stored credentials:" , error )
150+ this . log ( "[auth] Failed to parse stored credentials:" , error )
149151 }
150152 return null
151153 }
@@ -176,7 +178,7 @@ export class AuthService extends EventEmitter<AuthServiceEvents> {
176178 const url = `${ getRooCodeApiUrl ( ) } /extension/sign-in?${ params . toString ( ) } `
177179 await vscode . env . openExternal ( vscode . Uri . parse ( url ) )
178180 } catch ( error ) {
179- console . error ( `[auth] Error initiating Roo Code Cloud auth: ${ error } ` )
181+ this . log ( `[auth] Error initiating Roo Code Cloud auth: ${ error } ` )
180182 throw new Error ( `Failed to initiate Roo Code Cloud authentication: ${ error } ` )
181183 }
182184 }
@@ -201,7 +203,7 @@ export class AuthService extends EventEmitter<AuthServiceEvents> {
201203 const storedState = this . context . globalState . get ( AUTH_STATE_KEY )
202204
203205 if ( state !== storedState ) {
204- console . log ( "[auth] State mismatch in callback" )
206+ this . log ( "[auth] State mismatch in callback" )
205207 throw new Error ( "Invalid state parameter. Authentication request may have been tampered with." )
206208 }
207209
@@ -210,9 +212,9 @@ export class AuthService extends EventEmitter<AuthServiceEvents> {
210212 await this . storeCredentials ( credentials )
211213
212214 vscode . window . showInformationMessage ( "Successfully authenticated with Roo Code Cloud" )
213- console . log ( "[auth] Successfully authenticated with Roo Code Cloud" )
215+ this . log ( "[auth] Successfully authenticated with Roo Code Cloud" )
214216 } catch ( error ) {
215- console . log ( `[auth] Error handling Roo Code Cloud callback: ${ error } ` )
217+ this . log ( `[auth] Error handling Roo Code Cloud callback: ${ error } ` )
216218 const previousState = this . state
217219 this . state = "logged-out"
218220 this . emit ( "logged-out" , { previousState } )
@@ -237,14 +239,14 @@ export class AuthService extends EventEmitter<AuthServiceEvents> {
237239 try {
238240 await this . clerkLogout ( oldCredentials )
239241 } catch ( error ) {
240- console . error ( "[auth] Error calling clerkLogout:" , error )
242+ this . log ( "[auth] Error calling clerkLogout:" , error )
241243 }
242244 }
243245
244246 vscode . window . showInformationMessage ( "Logged out from Roo Code Cloud" )
245- console . log ( "[auth] Logged out from Roo Code Cloud" )
247+ this . log ( "[auth] Logged out from Roo Code Cloud" )
246248 } catch ( error ) {
247- console . log ( `[auth] Error logging out from Roo Code Cloud: ${ error } ` )
249+ this . log ( `[auth] Error logging out from Roo Code Cloud: ${ error } ` )
248250 throw new Error ( `Failed to log out from Roo Code Cloud: ${ error } ` )
249251 }
250252 }
@@ -281,7 +283,7 @@ export class AuthService extends EventEmitter<AuthServiceEvents> {
281283 */
282284 private async refreshSession ( ) : Promise < void > {
283285 if ( ! this . credentials ) {
284- console . log ( "[auth] Cannot refresh session: missing credentials" )
286+ this . log ( "[auth] Cannot refresh session: missing credentials" )
285287 this . state = "inactive-session"
286288 return
287289 }
@@ -292,12 +294,12 @@ export class AuthService extends EventEmitter<AuthServiceEvents> {
292294 this . state = "active-session"
293295
294296 if ( previousState !== "active-session" ) {
295- console . log ( "[auth] Transitioned to active-session state" )
297+ this . log ( "[auth] Transitioned to active-session state" )
296298 this . emit ( "active-session" , { previousState } )
297299 this . fetchUserInfo ( )
298300 }
299301 } catch ( error ) {
300- console . error ( "[auth] Failed to refresh session" , error )
302+ this . log ( "[auth] Failed to refresh session" , error )
301303 throw error
302304 }
303305 }
@@ -446,12 +448,12 @@ export class AuthService extends EventEmitter<AuthServiceEvents> {
446448 return this . _instance
447449 }
448450
449- static async createInstance ( context : vscode . ExtensionContext ) {
451+ static async createInstance ( context : vscode . ExtensionContext , log ?: ( ... args : unknown [ ] ) => void ) {
450452 if ( this . _instance ) {
451453 throw new Error ( "AuthService instance already created" )
452454 }
453455
454- this . _instance = new AuthService ( context )
456+ this . _instance = new AuthService ( context , log )
455457 await this . _instance . initialize ( )
456458 return this . _instance
457459 }
0 commit comments