|
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. |
@@ -120,36 +125,39 @@ export async function activate(context: vscode.ExtensionContext) { |
120 | 125 | // Initialize the provider *before* the Roo Code Cloud service. |
121 | 126 | const provider = new ClineProvider(context, outputChannel, "sidebar", contextProxy, mdmService) |
122 | 127 |
|
| 128 | + // Initialize Roo Code Cloud service. |
123 | 129 | const postStateListener = () => ClineProvider.getVisibleInstance()?.postStateToWebview() |
| 130 | + authStateChangedHandler = postStateListener |
| 131 | + settingsUpdatedHandler = postStateListener |
124 | 132 |
|
125 | | - // Initialize Roo Code Cloud service. |
126 | | - const cloudService = await CloudService.createInstance(context, cloudLogger, { |
127 | | - "auth-state-changed": postStateListener, |
128 | | - "settings-updated": postStateListener, |
129 | | - "user-info": async ({ userInfo }) => { |
130 | | - postStateListener() |
131 | | - |
132 | | - if (!CloudService.instance.cloudAPI) { |
133 | | - cloudLogger("[CloudService] CloudAPI is not initialized") |
134 | | - return |
135 | | - } |
| 133 | + userInfoHandler = async ({ userInfo }: { userInfo: any }) => { |
| 134 | + postStateListener() |
136 | 135 |
|
137 | | - try { |
138 | | - const config = await CloudService.instance.cloudAPI.bridgeConfig() |
139 | | - cloudLogger(`[CloudService] bridgeConfig -> ${JSON.stringify(config)}`) |
140 | | - |
141 | | - ExtensionBridgeService.handleRemoteControlState( |
142 | | - userInfo, |
143 | | - contextProxy.getValue("remoteControlEnabled"), |
144 | | - { ...config, provider, sessionId: vscode.env.sessionId }, |
145 | | - (message: string) => outputChannel.appendLine(message), |
146 | | - ) |
147 | | - } catch (error) { |
148 | | - cloudLogger( |
149 | | - `[CloudService] Failed to fetch bridgeConfig: ${error instanceof Error ? error.message : String(error)}`, |
150 | | - ) |
151 | | - } |
152 | | - }, |
| 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, |
153 | 161 | }) |
154 | 162 |
|
155 | 163 | try { |
@@ -293,6 +301,28 @@ export async function activate(context: vscode.ExtensionContext) { |
293 | 301 | export async function deactivate() { |
294 | 302 | outputChannel.appendLine(`${Package.name} extension deactivated`) |
295 | 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 | + |
296 | 326 | const bridgeService = ExtensionBridgeService.getInstance() |
297 | 327 |
|
298 | 328 | if (bridgeService) { |
|
0 commit comments