fix: do not process chat files for codegpt api

This commit is contained in:
Carl-Robert Linnupuu 2024-12-29 23:49:02 +00:00
parent 5a9c7c328e
commit 724870a4ed
17 changed files with 64 additions and 55 deletions

View file

@ -17,7 +17,6 @@ import ee.carlrobert.codegpt.conversations.message.Message;
import ee.carlrobert.codegpt.settings.configuration.ConfigurationSettings;
import ee.carlrobert.codegpt.toolwindow.chat.ChatToolWindowContentManager;
import ee.carlrobert.codegpt.ui.OverlayUtil;
import java.io.File;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
@ -66,7 +65,7 @@ public class ProjectCompilationStatusListener implements CompilationStatusListen
var message = new Message("Fix the following compile errors:\n\n" + prompt);
message.setReferencedFilePaths(errorMapping.keySet().stream()
.map(ReferencedFile::getFilePath)
.map(ReferencedFile::filePath)
.toList());
message.setPrompt(CompletionRequestUtil.getPromptWithContext(
new ArrayList<>(errorMapping.keySet()),
@ -77,7 +76,7 @@ public class ProjectCompilationStatusListener implements CompilationStatusListen
private HashMap<ReferencedFile, List<String>> getErrorMapping(CompileContext compileContext) {
var errorMapping = new HashMap<ReferencedFile, List<String>>();
for (var compilerMessage : compileContext.getMessages(CompilerMessageCategory.ERROR)) {
var key = new ReferencedFile(new File(compilerMessage.getVirtualFile().getPath()));
var key = ReferencedFile.from(compilerMessage.getVirtualFile());
var prevValue = errorMapping.get(key);
if (prevValue == null) {
prevValue = new ArrayList<>();

View file

@ -1,5 +1,7 @@
package ee.carlrobert.codegpt;
import com.intellij.openapi.vfs.VfsUtilCore;
import com.intellij.openapi.vfs.VirtualFile;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
@ -8,38 +10,38 @@ import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ReferencedFile {
public record ReferencedFile(String fileName, String filePath, String fileContent) {
private final String fileName;
private final String filePath;
private final String fileContent;
public static ReferencedFile from(File file) {
return new ReferencedFile(
file.getName(),
file.getPath(),
readContent(file)
);
}
public ReferencedFile(File file) {
this.fileName = file.getName();
this.filePath = file.getPath();
public static ReferencedFile from(VirtualFile virtualFile) {
return new ReferencedFile(
virtualFile.getName(),
virtualFile.getPath(),
readContent(virtualFile)
);
}
private static String readContent(File file) {
try {
this.fileContent = new String(Files.readAllBytes(Paths.get(filePath)));
return new String(Files.readAllBytes(Paths.get(file.getPath())));
} catch (IOException e) {
throw new RuntimeException(e);
throw new RuntimeException("Failed to read file content", e);
}
}
public ReferencedFile(String fileName, String filePath, String fileContent) {
this.fileName = fileName;
this.filePath = filePath;
this.fileContent = fileContent;
}
public String getFileName() {
return fileName;
}
public String getFilePath() {
return filePath;
}
public String getFileContent() {
return fileContent;
private static String readContent(VirtualFile virtualFile) {
try {
return VfsUtilCore.loadText(virtualFile);
} catch (IOException e) {
throw new RuntimeException("Failed to read virtual file content", e);
}
}
public String getFileExtension() {

View file

@ -167,7 +167,7 @@ public class IncludeFilesInContextAction extends AnAction {
private int calculateTotalTokens(List<ReferencedFile> referencedFiles) {
return referencedFiles.stream()
.mapToInt(file -> encodingManager.countTokens(file.getFileContent()))
.mapToInt(file -> encodingManager.countTokens(file.fileContent()))
.sum();
}
}

View file

@ -12,7 +12,6 @@ import com.intellij.openapi.project.Project;
import ee.carlrobert.codegpt.CodeGPTKeys;
import ee.carlrobert.codegpt.ReferencedFile;
import ee.carlrobert.codegpt.conversations.message.Message;
import ee.carlrobert.codegpt.settings.configuration.ConfigurationSettings;
import ee.carlrobert.codegpt.settings.prompts.PromptsSettings;
import ee.carlrobert.codegpt.toolwindow.chat.ChatToolWindowContentManager;
import ee.carlrobert.codegpt.util.file.FileUtil;
@ -69,7 +68,7 @@ public class EditorActionsUtil {
message.setReferencedFilePaths(
Stream.ofNullable(project.getUserData(CodeGPTKeys.SELECTED_FILES))
.flatMap(Collection::stream)
.map(ReferencedFile::getFilePath)
.map(ReferencedFile::filePath)
.toList());
toolWindowContentManager.sendMessage(message);
}

View file

@ -110,7 +110,7 @@ public class ChatToolWindowPanel extends SimpleToolWindowPanel {
}
var referencedFilePaths = referencedFiles.stream()
.map(ReferencedFile::getFilePath)
.map(ReferencedFile::filePath)
.toList();
selectedFilesNotification.show(
referencedFiles.size() + " files selected",

View file

@ -137,7 +137,7 @@ public class ChatToolWindowTabPanel implements Disposable {
})
.forEach(filePath -> {
try {
referencedFiles.put(filePath, new ReferencedFile(new File(filePath)));
referencedFiles.put(filePath, ReferencedFile.from(new File(filePath)));
} catch (Exception ex) {
LOG.error("Failed to create referenced file for path: " + filePath, ex);
}
@ -145,7 +145,7 @@ public class ChatToolWindowTabPanel implements Disposable {
List<ReferencedFile> selectedFiles = project.getUserData(CodeGPTKeys.SELECTED_FILES);
if (selectedFiles != null) {
selectedFiles.forEach(file -> referencedFiles.put(file.getFilePath(), file));
selectedFiles.forEach(file -> referencedFiles.put(file.filePath(), file));
}
return new ArrayList<>(referencedFiles.values());

View file

@ -131,7 +131,7 @@ public class TotalTokensPanel extends JPanel {
public void updateReferencedFilesTokens(List<ReferencedFile> includedFiles) {
totalTokensDetails.setReferencedFilesTokens(includedFiles.stream()
.mapToInt(file -> encodingManager.countTokens(file.getFileContent()))
.mapToInt(file -> encodingManager.countTokens(file.fileContent()))
.sum());
update();
}
@ -145,7 +145,7 @@ public class TotalTokensPanel extends JPanel {
tokenDetails.setConversationTokens(encodingManager.countConversationTokens(conversation));
if (includedFiles != null) {
tokenDetails.setReferencedFilesTokens(includedFiles.stream()
.mapToInt(file -> encodingManager.countTokens(file.getFileContent()))
.mapToInt(file -> encodingManager.countTokens(file.fileContent()))
.sum());
}
if (highlightedText != null) {

View file

@ -32,8 +32,7 @@ public class PsiElementCheckboxTree extends FileCheckboxTree {
}
return Arrays.stream(checkedNodes)
.map(item -> new ReferencedFile(
new File(item.getContainingFile().getVirtualFile().getPath())))
.map(item -> ReferencedFile.from(item.getContainingFile().getVirtualFile()))
.toList();
}

View file

@ -30,7 +30,7 @@ public class VirtualFileCheckboxTree extends FileCheckboxTree {
.map(item -> {
var file = new File(item.getPath());
if (file.isFile()) {
return new ReferencedFile(file);
return ReferencedFile.from(item);
}
return null;
})