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

Reimplement agent lifecycle management #303

Merged
merged 7 commits into from
Jan 17, 2024
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
40 changes: 0 additions & 40 deletions src/main/java/com/sourcegraph/cody/CodyAgentFocusListener.java

This file was deleted.

72 changes: 22 additions & 50 deletions src/main/java/com/sourcegraph/cody/CodyFileEditorListener.java
Original file line number Diff line number Diff line change
@@ -1,78 +1,50 @@
package com.sourcegraph.cody;

import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.fileEditor.FileDocumentManager;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.fileEditor.FileEditorManagerListener;
import com.intellij.openapi.util.Computable;
import com.intellij.openapi.vfs.VirtualFile;
import com.sourcegraph.cody.agent.CodyAgent;
import com.sourcegraph.cody.agent.CodyAgentClient;
import com.sourcegraph.cody.agent.CodyAgentServer;
import com.sourcegraph.cody.agent.CodyAgentCodebase;
import com.sourcegraph.cody.agent.CodyAgentService;
import com.sourcegraph.cody.agent.protocol.TextDocument;
import com.sourcegraph.config.ConfigUtil;
import java.util.concurrent.TimeUnit;
import org.jetbrains.annotations.NotNull;

public class CodyFileEditorListener implements FileEditorManagerListener {
private static final Logger logger = Logger.getInstance(CodyFileEditorListener.class);

@Override
public void fileOpened(@NotNull FileEditorManager source, @NotNull VirtualFile file) {
if (!ConfigUtil.isCodyEnabled()) {
return;
}
Document document = FileDocumentManager.getInstance().getDocument(file);
if (document == null) {
return;
}

ApplicationManager.getApplication()
.executeOnPooledThread(
() -> {
CodyAgent.getInitializedServer(source.getProject())
// The timeout has been increased from 3 to 12.
// This more like workaround than a fix to:
// https://github.com/sourcegraph/jetbrains/issues/169
Copy link
Contributor

Choose a reason for hiding this comment

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

I assume we can say that this PR actually fixes: #169

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There is a good chance for that.
Fas there any deterministic reproduction for the issue above?

Copy link
Contributor

Choose a reason for hiding this comment

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

I am afraid there was not 😕 I could have been related to the network

.completeOnTimeout(null, 12, TimeUnit.SECONDS)
.thenAccept(
server -> {
if (server == null) {
logger.warn("server is null");
ApplicationManager.getApplication()
.invokeLater(
() -> {
fileOpened(source, file);
});

return;
}
if (!CodyAgent.isConnected(source.getProject())) {
return;
}

server.textDocumentDidOpen(
TextDocument.fromPath(file.getPath(), document.getText()));

CodyAgentClient client = CodyAgent.getClient(source.getProject());
if (client.codebase == null) {
return;
}
client.codebase.onFileOpened(source.getProject(), file);
});
});
CodyAgentService.applyAgentOnBackgroundThread(
source.getProject(),
agent -> {
Document document =
ApplicationManager.getApplication()
.runReadAction(
(Computable<Document>)
() -> FileDocumentManager.getInstance().getDocument(file));
if (document != null) {
TextDocument textDocument = TextDocument.fromPath(file.getPath(), document.getText());
agent.getServer().textDocumentDidOpen(textDocument);
}
});

CodyAgentCodebase.getInstance(source.getProject()).onFileOpened(source.getProject(), file);
}

@Override
public void fileClosed(@NotNull FileEditorManager source, @NotNull VirtualFile file) {
if (!ConfigUtil.isCodyEnabled()) {
return;
}
CodyAgentServer server = CodyAgent.getServer(source.getProject());
if (server == null) {
return;
}
server.textDocumentDidClose(TextDocument.fromPath(file.getPath()));

CodyAgentService.applyAgentOnBackgroundThread(
source.getProject(),
agent -> agent.getServer().textDocumentDidClose(TextDocument.fromPath(file.getPath())));
}
}
55 changes: 55 additions & 0 deletions src/main/java/com/sourcegraph/cody/CodyFocusChangeListener.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.sourcegraph.cody;

import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.EditorFactory;
import com.intellij.openapi.editor.event.EditorEventMulticaster;
import com.intellij.openapi.editor.ex.EditorEventMulticasterEx;
import com.intellij.openapi.editor.ex.FocusChangeListener;
import com.intellij.openapi.fileEditor.FileDocumentManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.startup.StartupActivity;
import com.intellij.openapi.vfs.VirtualFile;
import com.sourcegraph.cody.agent.CodyAgentCodebase;
import com.sourcegraph.cody.agent.CodyAgentService;
import com.sourcegraph.cody.agent.protocol.TextDocument;
import com.sourcegraph.config.ConfigUtil;
import org.jetbrains.annotations.NotNull;

public final class CodyFocusChangeListener implements FocusChangeListener, StartupActivity {

@Override
public void runActivity(@NotNull Project project) {
EditorEventMulticaster multicaster = EditorFactory.getInstance().getEventMulticaster();
if (multicaster instanceof EditorEventMulticasterEx) {
try {
((EditorEventMulticasterEx) multicaster)
.addFocusChangeListener(this, CodyAgentService.getInstance(project));
} catch (Exception e) {
// Ignore exception https://github.com/sourcegraph/sourcegraph/issues/56032
}
}
}

@Override
public void focusGained(@NotNull Editor editor) {
if (!ConfigUtil.isCodyEnabled()) {
return;
}

Project project = editor.getProject();
if (project == null) {
return;
}

VirtualFile file = FileDocumentManager.getInstance().getFile(editor.getDocument());
if (file == null) {
return;
}

CodyAgentService.applyAgentOnBackgroundThread(
project,
agent -> agent.getServer().textDocumentDidFocus(TextDocument.fromPath(file.getPath())));

CodyAgentCodebase.getInstance(project).onFileOpened(project, file);
}
}
2 changes: 0 additions & 2 deletions src/main/java/com/sourcegraph/cody/agent/CodyAgentClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
public class CodyAgentClient {

private static final Logger logger = Logger.getInstance(CodyAgentClient.class);
@Nullable public CodyAgentServer server;
@Nullable public CodyAgentCodebase codebase;
// Callback that is invoked when the agent sends a "chat/updateMessageInProgress" notification.
@Nullable public Consumer<ChatMessage> onChatUpdateMessageInProgress;
@NotNull public Runnable onFinishedProcessing = () -> {};
Expand Down
20 changes: 0 additions & 20 deletions src/main/java/com/sourcegraph/config/ApiAuthenticator.java

This file was deleted.

5 changes: 3 additions & 2 deletions src/main/java/com/sourcegraph/telemetry/GraphQlLogger.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.google.gson.JsonObject;
import com.intellij.openapi.project.Project;
import com.sourcegraph.cody.agent.CodyAgent;
import com.sourcegraph.cody.agent.CodyAgentService;
import com.sourcegraph.cody.agent.protocol.Event;
import com.sourcegraph.cody.config.CodyApplicationSettings;
import com.sourcegraph.cody.config.SourcegraphServerPath;
Expand Down Expand Up @@ -65,7 +65,8 @@ private static JsonObject addGlobalEventParameters(
// This could be exposed later (as public), but currently, we don't use it externally.
private static CompletableFuture<Boolean> logEvent(
@NotNull Project project, @NotNull Event event) {
return CodyAgent.withServer(project, server -> server.logEvent(event))
return CodyAgentService.getAgent(project)
.thenApply(agent -> agent.getServer().logEvent(event))
.thenApply((ignored) -> true)
.exceptionally((ignored) -> false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.intellij.ide.plugins.PluginInstaller;
import com.intellij.ide.plugins.PluginStateListener;
import com.intellij.openapi.project.Project;
import com.sourcegraph.cody.agent.CodyAgentManager;
import com.sourcegraph.cody.config.CodyApplicationSettings;
import com.sourcegraph.cody.config.notification.AccountSettingChangeListener;
import com.sourcegraph.cody.config.notification.CodySettingChangeListener;
Expand Down Expand Up @@ -34,7 +33,6 @@ public void runActivity(@NotNull Project project) {
PluginInstaller.addStateListener(
new PluginStateListener() {
public void install(@NotNull IdeaPluginDescriptor ideaPluginDescriptor) {
CodyAgentManager.startAgent(project);
GraphQlLogger.logInstallEvent(project)
.thenAccept(
wasSuccessful -> {
Expand All @@ -46,7 +44,6 @@ public void install(@NotNull IdeaPluginDescriptor ideaPluginDescriptor) {

@Override
public void uninstall(@NotNull IdeaPluginDescriptor ideaPluginDescriptor) {
CodyAgentManager.stopAgent(project);
if (ideaPluginDescriptor
.getPluginId()
.getIdString()
Expand Down
9 changes: 5 additions & 4 deletions src/main/java/com/sourcegraph/vcs/RepoUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import com.intellij.openapi.vcs.ProjectLevelVcsManager;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.vcsUtil.VcsUtil;
import com.sourcegraph.cody.agent.CodyAgent;
import com.sourcegraph.cody.agent.CodyAgentService;
import com.sourcegraph.cody.agent.protocol.CloneURL;
import com.sourcegraph.cody.config.CodyProjectSettings;
import com.sourcegraph.common.ErrorNotification;
Expand Down Expand Up @@ -148,9 +148,10 @@ private static String doReplacements(
if (vcsType == VCSType.GIT && repository != null) {
String cloneURL = GitUtil.getRemoteRepoUrl((GitRepository) repository, project);
String codebaseName =
CodyAgent.withServer(
project,
server -> server.convertGitCloneURLToCodebaseName(new CloneURL(cloneURL)))
CodyAgentService.getAgent(project)
.thenCompose(
agent ->
agent.getServer().convertGitCloneURLToCodebaseName(new CloneURL(cloneURL)))
.join();
if (codebaseName == null) {
throw new Exception(
Expand Down
Loading