-
Notifications
You must be signed in to change notification settings - Fork 23
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
pkukielka
merged 7 commits into
main
from
pkukielka/reimplement-agent-lifecycle-management
Jan 17, 2024
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
6a2ff15
Reimplement agent lifecycle management
pkukielka 2a2d86e
Minor fixes
pkukielka 30206af
Refactor using spotless
pkukielka c39fc72
Fix initial repo setting
pkukielka c494d27
Few fixes for review
pkukielka 11fee97
Fix for test
pkukielka cab22bb
Reformat
pkukielka File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
40 changes: 0 additions & 40 deletions
40
src/main/java/com/sourcegraph/cody/CodyAgentFocusListener.java
This file was deleted.
Oops, something went wrong.
72 changes: 22 additions & 50 deletions
72
src/main/java/com/sourcegraph/cody/CodyFileEditorListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
.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
55
src/main/java/com/sourcegraph/cody/CodyFocusChangeListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 0 additions & 20 deletions
20
src/main/java/com/sourcegraph/config/ApiAuthenticator.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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