@@ -32,9 +32,14 @@ export class WakaTime {
32
32
private lastHeartbeat : number = 0 ;
33
33
private lastDebug : boolean = false ;
34
34
private lastCompile : boolean = false ;
35
+ private lastAICodeGenerating : boolean = false ;
35
36
private dedupe : FileSelectionMap = { } ;
36
37
private debounceTimeoutId : any = null ;
37
38
private debounceMs = 50 ;
39
+ private AIDebounceTimeoutId : any = null ;
40
+ private AIdebounceMs = 1000 ;
41
+ private AIdebounceCount = 0 ;
42
+ private AIpasteCount = 0 ;
38
43
private dependencies : Dependencies ;
39
44
private options : Options ;
40
45
private logger : Logger ;
@@ -48,6 +53,7 @@ export class WakaTime {
48
53
private extensionPath : string ;
49
54
private isCompiling : boolean = false ;
50
55
private isDebugging : boolean = false ;
56
+ private isAICodeGenerating : boolean = false ;
51
57
private currentlyFocusedFile : string ;
52
58
private teamDevsForFileCache = { } ;
53
59
private resourcesLocation : string ;
@@ -414,6 +420,7 @@ export class WakaTime {
414
420
// subscribe to selection change and editor activation events
415
421
let subscriptions : vscode . Disposable [ ] = [ ] ;
416
422
vscode . window . onDidChangeTextEditorSelection ( this . onChangeSelection , this , subscriptions ) ;
423
+ vscode . workspace . onDidChangeTextDocument ( this . onChangeTextDocument , this , subscriptions ) ;
417
424
vscode . window . onDidChangeActiveTextEditor ( this . onChangeTab , this , subscriptions ) ;
418
425
vscode . workspace . onDidSaveTextDocument ( this . onSave , this , subscriptions ) ;
419
426
@@ -460,6 +467,42 @@ export class WakaTime {
460
467
461
468
private onChangeSelection ( e : vscode . TextEditorSelectionChangeEvent ) : void {
462
469
if ( e . kind === vscode . TextEditorSelectionChangeKind . Command ) return ;
470
+ if ( Utils . isAIChatSidebar ( e . textEditor ?. document ?. uri ) ) {
471
+ this . isAICodeGenerating = true ;
472
+ }
473
+ this . onEvent ( false ) ;
474
+ }
475
+
476
+ private onChangeTextDocument ( e : vscode . TextDocumentChangeEvent ) : void {
477
+ if ( Utils . isAIChatSidebar ( e . document ?. uri ) ) {
478
+ this . isAICodeGenerating = true ;
479
+ this . AIdebounceCount = 0 ;
480
+ } else if ( e . contentChanges . length === 1 && e . contentChanges ?. [ 0 ] . text . length > 1 ) {
481
+ if ( this . AIpasteCount > 1 ) {
482
+ this . isAICodeGenerating = true ;
483
+ this . AIdebounceCount = 0 ;
484
+ }
485
+ this . AIpasteCount ++ ;
486
+ } else if (
487
+ this . isAICodeGenerating &&
488
+ e . contentChanges . length === 1 &&
489
+ e . contentChanges ?. [ 0 ] . text . length === 1 &&
490
+ e . contentChanges ?. [ 0 ] . text !== '\n' &&
491
+ e . contentChanges ?. [ 0 ] . text !== '\r'
492
+ ) {
493
+ this . AIdebounceCount ++ ;
494
+ clearTimeout ( this . AIDebounceTimeoutId ) ;
495
+ this . AIDebounceTimeoutId = setTimeout ( ( ) => {
496
+ if ( this . AIdebounceCount > 1 ) {
497
+ this . isAICodeGenerating = false ;
498
+ this . AIpasteCount = 0 ;
499
+ }
500
+ } , this . AIdebounceMs ) ;
501
+ } else if ( this . isAICodeGenerating ) {
502
+ this . AIdebounceCount = 0 ;
503
+ clearTimeout ( this . AIDebounceTimeoutId ) ;
504
+ }
505
+ if ( ! this . isAICodeGenerating ) return ;
463
506
this . onEvent ( false ) ;
464
507
}
465
508
@@ -500,7 +543,8 @@ export class WakaTime {
500
543
this . enoughTimePassed ( time ) ||
501
544
this . lastFile !== file ||
502
545
this . lastDebug !== this . isDebugging ||
503
- this . lastCompile !== this . isCompiling
546
+ this . lastCompile !== this . isCompiling ||
547
+ this . lastAICodeGenerating !== this . isAICodeGenerating
504
548
) {
505
549
this . sendHeartbeat (
506
550
doc ,
@@ -509,11 +553,13 @@ export class WakaTime {
509
553
isWrite ,
510
554
this . isCompiling ,
511
555
this . isDebugging ,
556
+ this . isAICodeGenerating ,
512
557
) ;
513
558
this . lastFile = file ;
514
559
this . lastHeartbeat = time ;
515
560
this . lastDebug = this . isDebugging ;
516
561
this . lastCompile = this . isCompiling ;
562
+ this . lastAICodeGenerating = this . isAICodeGenerating ;
517
563
}
518
564
}
519
565
}
@@ -528,10 +574,19 @@ export class WakaTime {
528
574
isWrite : boolean ,
529
575
isCompiling : boolean ,
530
576
isDebugging : boolean ,
577
+ isAICoding : boolean ,
531
578
) : Promise < void > {
532
579
const apiKey = await this . options . getApiKey ( ) ;
533
580
if ( apiKey ) {
534
- await this . _sendHeartbeat ( doc , time , selection , isWrite , isCompiling , isDebugging ) ;
581
+ await this . _sendHeartbeat (
582
+ doc ,
583
+ time ,
584
+ selection ,
585
+ isWrite ,
586
+ isCompiling ,
587
+ isDebugging ,
588
+ isAICoding ,
589
+ ) ;
535
590
} else {
536
591
await this . promptForApiKey ( ) ;
537
592
}
@@ -544,6 +599,7 @@ export class WakaTime {
544
599
isWrite : boolean ,
545
600
isCompiling : boolean ,
546
601
isDebugging : boolean ,
602
+ isAICoding : boolean ,
547
603
) : Promise < void > {
548
604
if ( ! this . dependencies . isCliInstalled ( ) ) return ;
549
605
@@ -572,6 +628,8 @@ export class WakaTime {
572
628
args . push ( '--category' , 'debugging' ) ;
573
629
} else if ( isCompiling ) {
574
630
args . push ( '--category' , 'building' ) ;
631
+ } else if ( isAICoding ) {
632
+ args . push ( '--category' , 'ai coding' ) ;
575
633
} else if ( Utils . isPullRequest ( doc . uri ) ) {
576
634
args . push ( '--category' , 'code reviewing' ) ;
577
635
}
0 commit comments