@@ -501,9 +501,7 @@ class CodeWhispererService(private val cs: CoroutineScope) : Disposable {
501501 return
502502 }
503503
504- if (response.nextToken().isEmpty()) {
505- CodeWhispererInvocationStatus .getInstance().finishInvocation()
506- }
504+ CodeWhispererInvocationStatus .getInstance().finishInvocation()
507505
508506 val caretMovement = CodeWhispererEditorManager .getInstance().getCaretMovement(
509507 requestContext.editor,
@@ -515,11 +513,12 @@ class CodeWhispererService(private val cs: CoroutineScope) : Disposable {
515513 // first response for the jobId
516514 nextStates = initStates(jobId, sessionContext, requestContext, responseContext, response, caretMovement, coroutine)
517515
518- // receiving a null state means caret has moved backward or there's a conflict with
519- // Intellisense popup, so we are going to cancel the current job
516+ // receiving a null state means caret has moved backward,
517+ // so we are going to cancel the current job
520518 if (nextStates == null ) {
521519 LOG .debug { " Exiting CodeWhisperer session. RequestId: $requestId " }
522- disposeJob(jobId)
520+ // buildInvalidInvocationContextForUTD(jobId, sessionContext, requestContext, responseContext, response.completions(), coroutine)
521+ // disposeDisplaySession(false)
523522 println (" exit 5 , jobId: $jobId " )
524523 return
525524 }
@@ -528,15 +527,17 @@ class CodeWhispererService(private val cs: CoroutineScope) : Disposable {
528527 nextStates = updateStates(currStates, response)
529528 }
530529 println (" adding ${response.completions().size} completions from job ${jobId} " )
531- ongoingRequests[jobId] = nextStates
530+ // ongoingRequests[jobId] = nextStates
532531
533- val hasAtLeastOneValid = checkRecommendationsValidity(jobId, nextStates, response.nextToken().isEmpty())
532+ // TODO: may have bug when it's a mix of auto-trigger + manual trigger
533+ val hasAtLeastOneValid = checkRecommendationsValidity(jobId, nextStates, true )
534534 val allSuggestions = ongoingRequests.values.filterNotNull().flatMap { it.recommendationContext.details }
535535 val valid = allSuggestions.filter { ! it.isDiscarded }.size
536536 println (" total: $valid valid, ${allSuggestions.size - valid} discarded" )
537537
538538 // If there are no recommendations at all in this session, we need to manually send the user decision event here
539539 // since it won't be sent automatically later
540+ // TODO: may have bug; visit later
540541 if (nextStates.recommendationContext.details.isEmpty() && response.nextToken().isEmpty()) {
541542 LOG .debug { " Received just an empty list from this session, requestId: $requestId " }
542543 CodeWhispererTelemetryService .getInstance().sendUserDecisionEvent(
@@ -557,14 +558,20 @@ class CodeWhispererService(private val cs: CoroutineScope) : Disposable {
557558 )
558559 }
559560 if (! hasAtLeastOneValid) {
560- if (response.nextToken().isEmpty()) {
561+ // if (response.nextToken().isEmpty()) {
561562 LOG .debug { " None of the recommendations are valid, exiting current CodeWhisperer pagination session" }
562563 // TODO: decide whether or not to dispose what here
563- disposeJob(jobId)
564- sessionContext.selectedIndex = CodeWhispererPopupManager .getInstance().findNewSelectedIndex(true , sessionContext.selectedIndex)
564+ // only key here, after disposing this, the whole session will also end
565+ if (ongoingRequests.keys.size == 1 ) {
566+ // buildInvalidInvocationContextForUTD(jobId, sessionContext, requestContext, responseContext, response.completions(), coroutine)
567+ disposeDisplaySession(false )
568+ } else {
569+ disposeJob(jobId)
570+ sessionContext.selectedIndex = CodeWhispererPopupManager .getInstance().findNewSelectedIndex(true , sessionContext.selectedIndex)
571+ }
565572 println (" exit 6 , jobId: $jobId " )
566573 return
567- }
574+ // }
568575 } else {
569576 updateCodeWhisperer(sessionContext, nextStates, isPopupShowing)
570577 }
@@ -586,7 +593,12 @@ class CodeWhispererService(private val cs: CoroutineScope) : Disposable {
586593
587594 if (caretMovement == CaretMovement .MOVE_BACKWARD ) {
588595 LOG .debug { " Caret moved backward, discarding all of the recommendations. Request ID: $requestId " }
589- sendDiscardedUserDecisionEventForAll(jobId, sessionContext, requestContext, responseContext, recommendations, coroutine)
596+ val detailContexts = recommendations.map {
597+ DetailContext (" " , it, it, true , false , " " , getCompletionType(it))
598+ }.toMutableList()
599+ val recommendationContext = RecommendationContext (detailContexts, " " , " " , VisualPosition (0 , 0 ), jobId)
600+ ongoingRequests[jobId] = buildInvocationContext(requestContext, responseContext, recommendationContext, coroutine)
601+ disposeDisplaySession(false )
590602 return null
591603 }
592604
@@ -614,7 +626,8 @@ class CodeWhispererService(private val cs: CoroutineScope) : Disposable {
614626 requestId
615627 )
616628 val recommendationContext = RecommendationContext (detailContexts, userInputOriginal, userInput, visualPosition, jobId)
617- return buildInvocationContext(requestContext, responseContext, recommendationContext, coroutine)
629+ ongoingRequests[jobId] = buildInvocationContext(requestContext, responseContext, recommendationContext, coroutine)
630+ return ongoingRequests[jobId]
618631 }
619632
620633 private fun updateStates (
@@ -653,7 +666,7 @@ class CodeWhispererService(private val cs: CoroutineScope) : Disposable {
653666 CodeWhispererPopupManager .getInstance().changeStatesForShowing(sessionContext, states, recommendationAdded)
654667 }
655668
656- private fun sendDiscardedUserDecisionEventForAll (
669+ private fun buildInvalidInvocationContextForUTD (
657670 jobId : Int ,
658671 sessionContext : SessionContext ,
659672 requestContext : RequestContext ,
@@ -667,7 +680,7 @@ class CodeWhispererService(private val cs: CoroutineScope) : Disposable {
667680 val recommendationContext = RecommendationContext (detailContexts, " " , " " , VisualPosition (0 , 0 ), jobId)
668681 ongoingRequests[jobId] = buildInvocationContext(requestContext, responseContext, recommendationContext, coroutine)
669682
670- CodeWhispererTelemetryService .getInstance().sendUserDecisionEventForAll(sessionContext, false )
683+ // CodeWhispererTelemetryService.getInstance().sendUserDecisionEventForAll(sessionContext, false)
671684 }
672685
673686 @RequiresEdt
0 commit comments