@@ -2,6 +2,7 @@ import { arch } from 'process';
22import type { ConfigurationChangeEvent } from 'vscode' ;
33import { Disposable , env , ProgressLocation , Uri , window , workspace } from 'vscode' ;
44import type { Container } from '../../../../container' ;
5+ import type { SubscriptionChangeEvent } from '../../../../plus/gk/subscriptionService' ;
56import { registerCommand } from '../../../../system/-webview/command' ;
67import { configuration } from '../../../../system/-webview/configuration' ;
78import { getContext } from '../../../../system/-webview/context' ;
@@ -27,6 +28,7 @@ export class GkCliIntegrationProvider implements Disposable {
2728 this . _disposable = Disposable . from (
2829 configuration . onDidChange ( e => this . onConfigurationChanged ( e ) ) ,
2930 ...this . registerCommands ( ) ,
31+ this . container . subscription . onDidChange ( this . onSubscriptionChanged , this ) ,
3032 ) ;
3133
3234 this . onConfigurationChanged ( ) ;
@@ -71,12 +73,11 @@ export class GkCliIntegrationProvider implements Disposable {
7173
7274 private async installMCPIfNeeded ( silent ?: boolean ) : Promise < void > {
7375 try {
74- if ( silent && this . container . storage . get ( 'ai:mcp:attemptInstall' , false ) ) {
76+ if ( silent && this . container . storage . get ( 'ai:mcp:attemptInstall' ) ) {
7577 return ;
7678 }
7779
78- // Store the flag to indicate that we have made the attempt
79- await this . container . storage . store ( 'ai:mcp:attemptInstall' , true ) ;
80+ await this . container . storage . store ( 'ai:mcp:attemptInstall' , 'attempted' ) ;
8081
8182 if ( configuration . get ( 'ai.enabled' ) === false ) {
8283 const message = 'Cannot install MCP: AI is disabled in settings' ;
@@ -292,8 +293,7 @@ export class GkCliIntegrationProvider implements Disposable {
292293
293294 const gkAuth = ( await this . container . subscription . getAuthenticationSession ( ) ) ?. accessToken ;
294295 if ( gkAuth != null ) {
295- const output = await run ( 'gk.exe' , [ 'auth' , 'login' , '-t' , gkAuth ] , 'utf8' , { cwd : mcpExtractedFolderPath . fsPath } ) ;
296- console . log ( output ) ;
296+ await run ( 'gk.exe' , [ 'auth' , 'login' , '-t' , gkAuth ] , 'utf8' , { cwd : mcpExtractedFolderPath . fsPath } ) ;
297297 }
298298 } catch {
299299 // Try alternative execution methods based on platform
@@ -377,6 +377,7 @@ export class GkCliIntegrationProvider implements Disposable {
377377 }
378378
379379 // Show success notification if not silent
380+ await this . container . storage . store ( 'ai:mcp:attemptInstall' , 'completed' ) ;
380381 void window . showInformationMessage ( 'GitKraken MCP integration installed successfully' ) ;
381382 } catch ( error ) {
382383 Logger . error ( `Error during MCP installation: ${ error } ` ) ;
@@ -388,7 +389,24 @@ export class GkCliIntegrationProvider implements Disposable {
388389 }
389390 }
390391
391- private registerCommands ( ) : Disposable [ ] {
392- return [ registerCommand ( 'gitlens.ai.mcp.install' , ( ) => this . installMCPIfNeeded ( ) ) ] ;
392+ private async onSubscriptionChanged ( e : SubscriptionChangeEvent ) : Promise < void > {
393+ const mcpInstallStatus = this . container . storage . get ( 'ai:mcp:attemptInstall' ) ;
394+ if ( e . current ?. account ?. id !== e . previous ?. account ?. id && mcpInstallStatus === 'completed' ) {
395+ try {
396+ await run ( 'gk' , [ 'auth' , 'logout' ] , 'utf8' ) ;
397+ } catch { }
398+ if ( e . current ?. account ?. id !== null ) {
399+ const currentSessionToken = ( await this . container . subscription . getAuthenticationSession ( ) ) ?. accessToken ;
400+ if ( currentSessionToken != null ) {
401+ try {
402+ await run ( 'gk' , [ 'auth' , 'login' , '-t' , currentSessionToken ] , 'utf8' ) ;
403+ } catch { }
404+ }
405+ }
393406 }
407+ }
408+
409+ private registerCommands ( ) : Disposable [ ] {
410+ return [ registerCommand ( 'gitlens.ai.mcp.install' , ( ) => this . installMCPIfNeeded ( ) ) ] ;
411+ }
394412}
0 commit comments