Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose Restart Cody action #1965

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 23 additions & 8 deletions src/main/kotlin/com/sourcegraph/cody/agent/CodyAgentService.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.sourcegraph.cody.agent

import com.intellij.notification.NotificationsManager
import com.intellij.openapi.Disposable
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.application.runInEdt
import com.intellij.openapi.components.Service
import com.intellij.openapi.components.service
import com.intellij.openapi.diagnostic.Logger
Expand All @@ -18,6 +20,7 @@ import com.sourcegraph.cody.error.CodyConsole
import com.sourcegraph.cody.ignore.IgnoreOracle
import com.sourcegraph.cody.listeners.CodyFileEditorListener
import com.sourcegraph.cody.statusbar.CodyStatusService
import com.sourcegraph.common.CodyBundle
import com.sourcegraph.utils.CodyEditorUtil
import java.util.Timer
import java.util.TimerTask
Expand Down Expand Up @@ -156,7 +159,7 @@ class CodyAgentService(private val project: Project) : Disposable {
synchronized(startupActions) { startupActions.add(action) }
}

fun startAgent(project: Project): CompletableFuture<CodyAgent> {
fun startAgent(project: Project, secondsTimeout: Long = 45): CompletableFuture<CodyAgent> {
ApplicationManager.getApplication().executeOnPooledThread {
try {
val future =
Expand All @@ -165,6 +168,7 @@ class CodyAgentService(private val project: Project) : Disposable {
logger.error(msg)
throw (CodyAgentException(msg))
}

val agent = future.get(45, TimeUnit.SECONDS)
if (!agent.isConnected()) {
val msg = "Failed to connect to agent Cody agent"
Expand All @@ -175,13 +179,24 @@ class CodyAgentService(private val project: Project) : Disposable {
codyAgent.complete(agent)
CodyStatusService.resetApplication(project)
}
} catch (e: TimeoutException) {
val msg = CodyBundle.getString("error.cody-connection-timeout.message")
runInEdt {
val isNoBalloonDisplayed =
NotificationsManager.getNotificationsManager()
.getNotificationsOfType(
CodyConnectionTimeoutExceptionNotification::class.java, project)
.all { it.balloon == null }
if (isNoBalloonDisplayed) {
CodyConnectionTimeoutExceptionNotification().notify(project)
}
}
setAgentError(project, msg)
codyAgent.completeExceptionally(CodyAgentException(msg, e))
} catch (e: Exception) {
val msg =
if (e is TimeoutException)
"Failed to start Cody agent in timely manner, please run any Cody action to retry"
else "Failed to start Cody agent"
logger.error(msg, e)
val msg = CodyBundle.getString("error.cody-starting.message")
setAgentError(project, msg)
logger.error(msg, e)
codyAgent.completeExceptionally(CodyAgentException(msg, e))
}
}
Expand All @@ -200,10 +215,10 @@ class CodyAgentService(private val project: Project) : Disposable {
}
}

fun restartAgent(project: Project): CompletableFuture<CodyAgent> {
fun restartAgent(project: Project, secondsTimeout: Long = 90): CompletableFuture<CodyAgent> {
synchronized(this) {
stopAgent(project)
return startAgent(project)
return startAgent(project, secondsTimeout)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.sourcegraph.cody.agent

import com.intellij.notification.Notification
import com.intellij.notification.NotificationType
import com.intellij.notification.impl.NotificationFullContent
import com.sourcegraph.Icons
import com.sourcegraph.cody.agent.action.CodyAgentRestartAction
import com.sourcegraph.common.CodyBundle
import com.sourcegraph.common.NotificationGroups

class CodyConnectionTimeoutExceptionNotification :
Notification(
NotificationGroups.SOURCEGRAPH_ERRORS,
CodyBundle.getString("notifications.cody-connection-timeout.title"),
CodyBundle.getString("notifications.cody-connection-timeout.detail"),
NotificationType.WARNING),
NotificationFullContent {

init {
icon = Icons.CodyLogoSlash
addAction(CodyAgentRestartAction())
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
package com.sourcegraph.cody.agent.action

import com.intellij.notification.NotificationsManager
import com.intellij.openapi.actionSystem.AnActionEvent
import com.sourcegraph.cody.agent.CodyAgentService
import com.sourcegraph.cody.agent.CodyConnectionTimeoutExceptionNotification
import com.sourcegraph.common.ui.DumbAwareEDTAction

class CodyAgentRestartAction : DumbAwareEDTAction("Restart Cody Agent") {
class CodyAgentRestartAction : DumbAwareEDTAction("Restart Cody") {
override fun actionPerformed(event: AnActionEvent) {
event.project?.let { CodyAgentService.getInstance(it).restartAgent(it) }
event.project?.let { project ->
CodyAgentService.getInstance(project).restartAgent(project)
NotificationsManager.getNotificationsManager()
.getNotificationsOfType(CodyConnectionTimeoutExceptionNotification::class.java, project)
.forEach { it.expire() }
}
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.sourcegraph.cody.statusbar

import com.intellij.ide.actions.AboutAction
import com.intellij.openapi.actionSystem.ActionUpdateThread
import com.intellij.openapi.actionSystem.AnActionEvent
import com.intellij.openapi.actionSystem.DefaultActionGroup
import com.sourcegraph.cody.agent.action.CodyAgentRestartAction
import com.sourcegraph.common.CodyBundle
import com.sourcegraph.common.CodyBundle.fmt
import com.sourcegraph.common.UpgradeToCodyProNotification
Expand All @@ -20,14 +20,11 @@ class CodyStatusBarActionGroup : DefaultActionGroup() {
e.presentation.isVisible = ConfigUtil.isCodyEnabled()

removeAll()
if (e.project?.let { CodyStatusService.getCurrentStatus(it) } ==
CodyStatus.CodyAgentNotRunning) {
addAll(
OpenLogAction(),
AboutAction().apply { templatePresentation.text = "Open About To Troubleshoot Issue" },
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AboutAction here seems to be redundant. It makes the action group overpopulated. The GitHub report fills the about info automatically.

ReportCodyBugAction())
val status = e.project?.let { CodyStatusService.getCurrentStatus(it) }
if (status == CodyStatus.CodyAgentNotRunning || status == CodyStatus.AgentError) {
addAll(CodyAgentRestartAction(), OpenLogAction(), ReportCodyBugAction())
} else {
addAll(listOfNotNull(deriveWarningAction()))
addAll(listOfNotNull(deriveRateLimitErrorAction()))
addSeparator()
addAll(
CodyManageAccountsAction(),
Expand All @@ -43,7 +40,7 @@ class CodyStatusBarActionGroup : DefaultActionGroup() {
}
}

private fun deriveWarningAction(): RateLimitErrorWarningAction? {
private fun deriveRateLimitErrorAction(): RateLimitErrorWarningAction? {
val autocompleteRLE = UpgradeToCodyProNotification.autocompleteRateLimitError.get()
val chatRLE = UpgradeToCodyProNotification.chatRateLimitError.get()

Expand Down
6 changes: 4 additions & 2 deletions src/main/resources/CodyBundle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,10 @@ notification.general.cody-not-running.title=Cody is starting...
notification.general.cody-not-running.detail=Please wait a few seconds. If Cody still won't start, check the IDE logs for errors.
notifications.edits.editing-not-available.title=Cody can't edit the file
notifications.edits.editing-not-available.detail=This could be because the file is not writable or Cody has trouble communicating with the editor.

notifications.cody-connection-timeout.title=Cody's connection timeout
notifications.cody-connection-timeout.detail=Cody took longer than expected to start. Please run any Cody action or restart Cody to retry.
error.cody-connection-timeout.message=Failed to start Cody in timely manner, please run any Cody action to retry
error.cody-starting.message=Failed to start Cody
# Context Filters
filter.action-in-ignored-file.detail=This file has been restricted by an admin. Autocomplete, commands, and other Cody features are disabled.
filter.action-in-ignored-file.learn-more-cta=Learn about Context Filters
Expand All @@ -186,7 +189,6 @@ filter.sidebar-panel-ignored-file.learn-more-cta=Learn more

# Other Actions
action.cody.not-working=Cody is disabled or still starting up
action.cody.restartAgent.text=Restart Cody Agent
chat.enhanced_context.title=Chat Context Settings
action.sourcegraph.disabled.description=Log in to Sourcegraph to enable Cody features

Expand Down
8 changes: 4 additions & 4 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -373,11 +373,11 @@
</group>

<group id="Cody.Other" text="Other" description="Other Cody actions">
<action id="cody.restartAgent"
<action id="cody.restartCody"
class="com.sourcegraph.cody.agent.action.CodyAgentRestartAction"
text="Restart Cody Agent"
description="Restarts the Cody agent">
<override-text place="GoToAction" text="Cody: Restart Cody Agent"/>
text="Restart Cody"
description="Restarts Cody">
<override-text place="GoToAction" text="Cody: Restart Cody"/>
</action>
<action id="sourcegraph.login"
class="com.sourcegraph.config.OpenPluginSettingsAction"
Expand Down
Loading