mirror of
https://github.com/carlrobertoh/ProxyAI.git
synced 2026-05-19 16:28:46 +00:00
fix: do not process chat files for codegpt api
This commit is contained in:
parent
5a9c7c328e
commit
724870a4ed
17 changed files with 64 additions and 55 deletions
|
|
@ -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<>();
|
||||
|
|
|
|||
|
|
@ -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() {
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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",
|
||||
|
|
|
|||
|
|
@ -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());
|
||||
|
|
|
|||
|
|
@ -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) {
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
})
|
||||
|
|
|
|||
|
|
@ -15,12 +15,12 @@ object CompletionRequestUtil {
|
|||
val repeatableContext = referencedFiles.stream()
|
||||
.map { item: ReferencedFile ->
|
||||
includedFilesSettings.repeatableContext
|
||||
.replace("{FILE_PATH}", item.filePath)
|
||||
.replace("{FILE_PATH}", item.filePath())
|
||||
.replace(
|
||||
"{FILE_CONTENT}", String.format(
|
||||
"```%s%n%s%n```",
|
||||
item.fileExtension,
|
||||
item.fileContent.trim { it <= ' ' })
|
||||
item.fileContent().trim { it <= ' ' })
|
||||
)
|
||||
}
|
||||
.collect(Collectors.joining("\n\n"))
|
||||
|
|
|
|||
|
|
@ -17,7 +17,7 @@ class CodeGPTRequestFactory : BaseRequestFactory() {
|
|||
val model = service<CodeGPTServiceSettings>().state.chatCompletionSettings.model
|
||||
val configuration = service<ConfigurationSettings>().state
|
||||
val requestBuilder: ChatCompletionRequest.Builder =
|
||||
ChatCompletionRequest.Builder(buildOpenAIMessages(model, params))
|
||||
ChatCompletionRequest.Builder(buildOpenAIMessages(model, params, emptyList()))
|
||||
.setModel(model)
|
||||
.setSessionId(params.sessionId)
|
||||
.setStream(true)
|
||||
|
|
@ -49,7 +49,7 @@ class CodeGPTRequestFactory : BaseRequestFactory() {
|
|||
}
|
||||
params.referencedFiles?.let {
|
||||
val fileContexts = it.map { file ->
|
||||
ContextFile(file.fileName, file.fileContent)
|
||||
ContextFile(file.fileName(), file.fileContent())
|
||||
}
|
||||
requestBuilder.setContext(AdditionalRequestContext(fileContexts))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ package ee.carlrobert.codegpt.completions.factory
|
|||
|
||||
import com.intellij.openapi.components.service
|
||||
import ee.carlrobert.codegpt.EncodingManager
|
||||
import ee.carlrobert.codegpt.ReferencedFile
|
||||
import ee.carlrobert.codegpt.completions.*
|
||||
import ee.carlrobert.codegpt.conversations.ConversationsState
|
||||
import ee.carlrobert.codegpt.settings.configuration.ConfigurationSettings
|
||||
|
|
@ -106,8 +107,9 @@ class OpenAIRequestFactory : CompletionRequestFactory {
|
|||
fun buildOpenAIMessages(
|
||||
model: String?,
|
||||
callParameters: ChatCompletionParameters,
|
||||
referencedFiles: List<ReferencedFile>? = null
|
||||
): List<OpenAIChatCompletionMessage> {
|
||||
val messages = buildOpenAIChatMessages(model, callParameters)
|
||||
val messages = buildOpenAIChatMessages(model, callParameters, referencedFiles ?: callParameters.referencedFiles)
|
||||
|
||||
if (model == null) {
|
||||
return messages
|
||||
|
|
@ -142,6 +144,7 @@ class OpenAIRequestFactory : CompletionRequestFactory {
|
|||
private fun buildOpenAIChatMessages(
|
||||
model: String?,
|
||||
callParameters: ChatCompletionParameters,
|
||||
referencedFiles: List<ReferencedFile>? = null
|
||||
): MutableList<OpenAIChatCompletionMessage> {
|
||||
val message = callParameters.message
|
||||
val messages = mutableListOf<OpenAIChatCompletionMessage>()
|
||||
|
|
@ -225,11 +228,11 @@ class OpenAIRequestFactory : CompletionRequestFactory {
|
|||
)
|
||||
)
|
||||
} else {
|
||||
val prompt = if (callParameters.referencedFiles.isNullOrEmpty()) {
|
||||
val prompt = if (referencedFiles.isNullOrEmpty()) {
|
||||
message.prompt
|
||||
} else {
|
||||
CompletionRequestUtil.getPromptWithContext(
|
||||
callParameters.referencedFiles!!,
|
||||
referencedFiles,
|
||||
message.prompt
|
||||
)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ package ee.carlrobert.codegpt.predictions
|
|||
|
||||
import com.intellij.codeInsight.hint.HintManagerImpl
|
||||
import com.intellij.openapi.actionSystem.DataContext
|
||||
import com.intellij.openapi.components.service
|
||||
import com.intellij.openapi.editor.Caret
|
||||
import com.intellij.openapi.editor.Editor
|
||||
import com.intellij.openapi.editor.actionSystem.EditorAction
|
||||
|
|
@ -17,11 +18,7 @@ class AcceptNextPredictionRevisionAction : EditorAction(Handler()), HintManagerI
|
|||
private class Handler : EditorWriteActionHandler() {
|
||||
|
||||
override fun doExecute(editor: Editor, caret: Caret?, dataContext: DataContext?) {
|
||||
val diffViewer = editor.getUserData(CodeGPTKeys.EDITOR_PREDICTION_DIFF_VIEWER)
|
||||
if (diffViewer != null && diffViewer.isVisible()) {
|
||||
diffViewer.applyChanges()
|
||||
return
|
||||
}
|
||||
service<PredictionService>().acceptPrediction(editor)
|
||||
}
|
||||
|
||||
override fun isEnabledForCaret(editor: Editor, caret: Caret, dataContext: DataContext): Boolean {
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ import com.intellij.openapi.application.runReadAction
|
|||
import com.intellij.openapi.components.Service
|
||||
import com.intellij.openapi.components.service
|
||||
import com.intellij.openapi.editor.Editor
|
||||
import ee.carlrobert.codegpt.CodeGPTKeys
|
||||
import ee.carlrobert.codegpt.CodeGPTKeys.IS_FETCHING_COMPLETION
|
||||
import ee.carlrobert.codegpt.CodeGPTKeys.PENDING_PREDICTION_CALL
|
||||
import ee.carlrobert.codegpt.codecompletions.CodeCompletionProgressNotifier
|
||||
|
|
@ -55,6 +56,14 @@ class PredictionService {
|
|||
displayInlineDiff(editor, request)
|
||||
}
|
||||
|
||||
fun acceptPrediction(editor: Editor) {
|
||||
val diffViewer = editor.getUserData(CodeGPTKeys.EDITOR_PREDICTION_DIFF_VIEWER)
|
||||
if (diffViewer != null && diffViewer.isVisible()) {
|
||||
diffViewer.applyChanges()
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
private fun displayInlineDiff(editor: Editor, request: Request) {
|
||||
val prediction = getPrediction(editor, request)
|
||||
if (prediction != null && !prediction.nextRevision.isNullOrEmpty()) {
|
||||
|
|
|
|||
|
|
@ -159,8 +159,8 @@ class OllamaSettingsForm {
|
|||
.build()
|
||||
.modelTags
|
||||
.models
|
||||
.map { it.name }
|
||||
.sortedWith(compareBy({ it.split(":").first() }, {
|
||||
?.map { it.name }
|
||||
?.sortedWith(compareBy({ it.split(":").first() }, {
|
||||
if (it.contains("latest")) 1 else 0
|
||||
}))
|
||||
} catch (t: Throwable) {
|
||||
|
|
|
|||
|
|
@ -29,7 +29,7 @@ class MessageBuilder(private val project: Project, private val text: String) {
|
|||
|
||||
fun withReferencedFiles(referencedFiles: List<ReferencedFile>): MessageBuilder {
|
||||
if (referencedFiles.isNotEmpty()) {
|
||||
message.referencedFilePaths = referencedFiles.map { it.filePath }
|
||||
message.referencedFilePaths = referencedFiles.map { it.filePath() }
|
||||
}
|
||||
return this
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@ import com.intellij.openapi.components.Service
|
|||
import com.intellij.openapi.diagnostic.thisLogger
|
||||
import com.intellij.openapi.project.Project
|
||||
import com.intellij.openapi.vfs.VirtualFile
|
||||
import com.intellij.openapi.vfs.readText
|
||||
import ee.carlrobert.codegpt.CodeGPTKeys
|
||||
import ee.carlrobert.codegpt.ReferencedFile
|
||||
import ee.carlrobert.codegpt.actions.IncludeFilesInContextNotifier
|
||||
|
|
@ -25,7 +26,7 @@ class FileSearchService private constructor(val project: Project) {
|
|||
project.getUserData(CodeGPTKeys.SELECTED_FILES).orEmpty().toMutableList()
|
||||
files.forEach { file ->
|
||||
try {
|
||||
filesIncluded.add(ReferencedFile(File(file.path)))
|
||||
filesIncluded.add(ReferencedFile(file.name, file.path, file.readText()))
|
||||
} catch (e: Exception) {
|
||||
logger.error("Failed to add file to session", e)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue