Skip to content

Commit eaa7258

Browse files
authored
Fix the issue when CodeWhisperer invocation is not set to complete when editor is disposed (#3762)
1 parent 7b43dbe commit eaa7258

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"type" : "bugfix",
3+
"description" : "CodeWhisperer: Fix an issue where CodeWhisperer would stuck in the invocation state indefinitely"
4+
}

jetbrains-core/src/software/aws/toolkits/jetbrains/services/codewhisperer/service/CodeWhispererInvocationStatus.kt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import java.util.concurrent.atomic.AtomicBoolean
1515

1616
class CodeWhispererInvocationStatus {
1717
private val isInvokingCodeWhisperer: AtomicBoolean = AtomicBoolean(false)
18+
private var invokingSessionId: String? = null
1819
private var timeAtLastInvocationComplete: Instant? = null
1920
var timeAtLastDocumentChanged: Instant = Instant.now()
2021
private set
@@ -25,7 +26,7 @@ class CodeWhispererInvocationStatus {
2526

2627
fun checkExistingInvocationAndSet(): Boolean =
2728
if (isInvokingCodeWhisperer.getAndSet(true)) {
28-
LOG.debug { "Have existing CodeWhisperer invocation" }
29+
LOG.debug { "Have existing CodeWhisperer invocation, sessionId: $invokingSessionId" }
2930
true
3031
} else {
3132
ApplicationManager.getApplication().messageBus.syncPublisher(CODEWHISPERER_INVOCATION_STATE_CHANGED).invocationStateChanged(true)
@@ -39,6 +40,7 @@ class CodeWhispererInvocationStatus {
3940
if (isInvokingCodeWhisperer.compareAndSet(true, false)) {
4041
ApplicationManager.getApplication().messageBus.syncPublisher(CODEWHISPERER_INVOCATION_STATE_CHANGED).invocationStateChanged(false)
4142
LOG.debug { "Ending CodeWhisperer invocation" }
43+
invokingSessionId = null
4244
}
4345
}
4446

@@ -75,6 +77,11 @@ class CodeWhispererInvocationStatus {
7577
timeAtLastInvocationStart = Instant.now()
7678
}
7779

80+
fun setInvocationSessionId(sessionId: String?) {
81+
LOG.debug { "Set current CodeWhisperer invocation sessionId: $sessionId" }
82+
invokingSessionId = sessionId
83+
}
84+
7885
fun hasEnoughDelayToInvokeCodeWhisperer(): Boolean {
7986
val timeCanShowCodeWhisperer = timeAtLastInvocationStart?.plusMillis(CodeWhispererConstants.INVOCATION_INTERVAL) ?: return true
8087
return timeCanShowCodeWhisperer.isBefore(Instant.now())

jetbrains-core/src/software/aws/toolkits/jetbrains/services/codewhisperer/service/CodeWhispererService.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,15 +170,16 @@ class CodeWhispererService {
170170
val latency = TimeUnit.NANOSECONDS.toMillis(endTime - startTime).toDouble()
171171
startTime = endTime
172172
val requestId = response.responseMetadata().requestId()
173+
val sessionId = response.sdkHttpResponse().headers().getOrDefault(KET_SESSION_ID, listOf(requestId))[0]
173174
if (requestCount == 1) {
174175
requestContext.latencyContext.codewhispererPostprocessingStart = System.nanoTime()
175176
requestContext.latencyContext.paginationFirstCompletionTime = latency
176177
requestContext.latencyContext.firstRequestId = requestId
178+
CodeWhispererInvocationStatus.getInstance().setInvocationSessionId(sessionId)
177179
}
178180
if (response.nextToken().isEmpty()) {
179181
requestContext.latencyContext.paginationAllCompletionsEnd = System.nanoTime()
180182
}
181-
val sessionId = response.sdkHttpResponse().headers().getOrDefault(KET_SESSION_ID, listOf(requestId))[0]
182183
val emptyRecommendations = checkEmptyRecommendations(response.completions())
183184
val completionType = checkCompletionType(response.completions(), emptyRecommendations)
184185
val responseContext = ResponseContext(sessionId, completionType)
@@ -269,6 +270,7 @@ class CodeWhispererService {
269270
}
270271
val exceptionType = e::class.simpleName
271272
val responseContext = ResponseContext(sessionId, CodewhispererCompletionType.Unknown)
273+
CodeWhispererInvocationStatus.getInstance().setInvocationSessionId(sessionId)
272274
logServiceInvocation(requestId, requestContext, responseContext, emptyList(), null, exceptionType)
273275
CodeWhispererTelemetryService.getInstance().sendServiceInvocationEvent(
274276
requestId,
@@ -332,6 +334,7 @@ class CodeWhispererService {
332334

333335
if (requestContext.editor.isDisposed) {
334336
LOG.debug { "Stop showing CodeWhisperer recommendations since editor is disposed. RequestId: $requestId" }
337+
CodeWhispererPopupManager.getInstance().cancelPopup(popup)
335338
return null
336339
}
337340

0 commit comments

Comments
 (0)