12
12
console . warn ( "Failed to load environment variables:" , e )
13
13
}
14
14
15
- import { CloudService , ExtensionBridgeService } from "@roo-code/cloud"
15
+ import { CloudService , ExtensionBridgeService , type CloudUserInfo } from "@roo-code/cloud"
16
16
import { TelemetryService , PostHogTelemetryClient } from "@roo-code/telemetry"
17
17
18
18
import "./utils/path" // Necessary to have access to String.prototype.toPosix.
@@ -51,6 +51,11 @@ import { initializeI18n } from "./i18n"
51
51
52
52
let outputChannel : vscode . OutputChannel
53
53
let extensionContext : vscode . ExtensionContext
54
+ let cloudService : CloudService | undefined
55
+
56
+ let authStateChangedHandler : ( ( ) => void ) | undefined
57
+ let settingsUpdatedHandler : ( ( ) => void ) | undefined
58
+ let userInfoHandler : ( ( data : { userInfo : CloudUserInfo } ) => Promise < void > ) | undefined
54
59
55
60
// This method is called when your extension is activated.
56
61
// Your extension is activated the very first time the command is executed.
@@ -94,27 +99,66 @@ export async function activate(context: vscode.ExtensionContext) {
94
99
95
100
const contextProxy = await ContextProxy . getInstance ( context )
96
101
97
- // Initialize code index managers for all workspace folders
102
+ // Initialize code index managers for all workspace folders.
98
103
const codeIndexManagers : CodeIndexManager [ ] = [ ]
104
+
99
105
if ( vscode . workspace . workspaceFolders ) {
100
106
for ( const folder of vscode . workspace . workspaceFolders ) {
101
107
const manager = CodeIndexManager . getInstance ( context , folder . uri . fsPath )
108
+
102
109
if ( manager ) {
103
110
codeIndexManagers . push ( manager )
111
+
104
112
try {
105
113
await manager . initialize ( contextProxy )
106
114
} catch ( error ) {
107
115
outputChannel . appendLine (
108
116
`[CodeIndexManager] Error during background CodeIndexManager configuration/indexing for ${ folder . uri . fsPath } : ${ error . message || error } ` ,
109
117
)
110
118
}
119
+
111
120
context . subscriptions . push ( manager )
112
121
}
113
122
}
114
123
}
115
124
125
+ // Initialize the provider *before* the Roo Code Cloud service.
126
+ const provider = new ClineProvider ( context , outputChannel , "sidebar" , contextProxy , mdmService )
127
+
116
128
// Initialize Roo Code Cloud service.
117
- const cloudService = await CloudService . createInstance ( context , cloudLogger )
129
+ const postStateListener = ( ) => ClineProvider . getVisibleInstance ( ) ?. postStateToWebview ( )
130
+ authStateChangedHandler = postStateListener
131
+ settingsUpdatedHandler = postStateListener
132
+
133
+ userInfoHandler = async ( { userInfo } : { userInfo : CloudUserInfo } ) => {
134
+ postStateListener ( )
135
+
136
+ if ( ! CloudService . instance . cloudAPI ) {
137
+ cloudLogger ( "[CloudService] CloudAPI is not initialized" )
138
+ return
139
+ }
140
+
141
+ try {
142
+ const config = await CloudService . instance . cloudAPI . bridgeConfig ( )
143
+
144
+ ExtensionBridgeService . handleRemoteControlState (
145
+ userInfo ,
146
+ contextProxy . getValue ( "remoteControlEnabled" ) ,
147
+ { ...config , provider, sessionId : vscode . env . sessionId } ,
148
+ ( message : string ) => outputChannel . appendLine ( message ) ,
149
+ )
150
+ } catch ( error ) {
151
+ cloudLogger (
152
+ `[CloudService] Failed to fetch bridgeConfig: ${ error instanceof Error ? error . message : String ( error ) } ` ,
153
+ )
154
+ }
155
+ }
156
+
157
+ cloudService = await CloudService . createInstance ( context , cloudLogger , {
158
+ "auth-state-changed" : authStateChangedHandler ,
159
+ "settings-updated" : settingsUpdatedHandler ,
160
+ "user-info" : userInfoHandler ,
161
+ } )
118
162
119
163
try {
120
164
if ( cloudService . telemetryClient ) {
@@ -126,33 +170,10 @@ export async function activate(context: vscode.ExtensionContext) {
126
170
)
127
171
}
128
172
129
- const postStateListener = ( ) => ClineProvider . getVisibleInstance ( ) ?. postStateToWebview ( )
130
-
131
- cloudService . on ( "auth-state-changed" , postStateListener )
132
- cloudService . on ( "settings-updated" , postStateListener )
133
-
134
- cloudService . on ( "user-info" , async ( { userInfo } ) => {
135
- postStateListener ( )
136
-
137
- const bridgeConfig = await cloudService . cloudAPI ?. bridgeConfig ( ) . catch ( ( ) => undefined )
138
-
139
- if ( ! bridgeConfig ) {
140
- outputChannel . appendLine ( "[CloudService] Failed to get bridge config" )
141
- return
142
- }
143
-
144
- ExtensionBridgeService . handleRemoteControlState (
145
- userInfo ,
146
- contextProxy . getValue ( "remoteControlEnabled" ) ,
147
- { ...bridgeConfig , provider, sessionId : vscode . env . sessionId } ,
148
- ( message : string ) => outputChannel . appendLine ( message ) ,
149
- )
150
- } )
151
-
152
173
// Add to subscriptions for proper cleanup on deactivate.
153
174
context . subscriptions . push ( cloudService )
154
175
155
- const provider = new ClineProvider ( context , outputChannel , "sidebar" , contextProxy , mdmService )
176
+ // Finish initializing the provider.
156
177
TelemetryService . instance . setProvider ( provider )
157
178
158
179
context . subscriptions . push (
@@ -280,6 +301,28 @@ export async function activate(context: vscode.ExtensionContext) {
280
301
export async function deactivate ( ) {
281
302
outputChannel . appendLine ( `${ Package . name } extension deactivated` )
282
303
304
+ if ( cloudService && CloudService . hasInstance ( ) ) {
305
+ try {
306
+ if ( authStateChangedHandler ) {
307
+ CloudService . instance . off ( "auth-state-changed" , authStateChangedHandler )
308
+ }
309
+
310
+ if ( settingsUpdatedHandler ) {
311
+ CloudService . instance . off ( "settings-updated" , settingsUpdatedHandler )
312
+ }
313
+
314
+ if ( userInfoHandler ) {
315
+ CloudService . instance . off ( "user-info" , userInfoHandler as any )
316
+ }
317
+
318
+ outputChannel . appendLine ( "CloudService event handlers cleaned up" )
319
+ } catch ( error ) {
320
+ outputChannel . appendLine (
321
+ `Failed to clean up CloudService event handlers: ${ error instanceof Error ? error . message : String ( error ) } ` ,
322
+ )
323
+ }
324
+ }
325
+
283
326
const bridgeService = ExtensionBridgeService . getInstance ( )
284
327
285
328
if ( bridgeService ) {
0 commit comments