From 7767e0692af051daf896f4c4d031570466a0c69d Mon Sep 17 00:00:00 2001 From: Jiatong Li Date: Sun, 18 May 2025 22:35:43 -0700 Subject: [PATCH] feat(amazonq): add fileUri to FileContext --- .../codewhisperer/editor/CodeWhispererEditorUtil.kt | 8 +++++++- .../services/codewhisperer/model/CodeWhispererModel.kt | 1 + .../codewhisperer/service/CodeWhispererService.kt | 1 + .../codewhisperer/service/CodeWhispererServiceNew.kt | 1 + .../codewhisperer/CodeWhispererCodeCoverageTrackerTest.kt | 2 +- .../codewhisperer/CodeWhispererFileContextProviderTest.kt | 2 ++ .../services/codewhisperer/CodeWhispererServiceTest.kt | 4 +++- .../services/codewhisperer/CodeWhispererTestUtil.kt | 3 ++- .../codegen-resources/codewhispererruntime/service-2.json | 7 +++++++ 9 files changed, 25 insertions(+), 4 deletions(-) diff --git a/plugins/amazonq/codewhisperer/jetbrains-community/src/software/aws/toolkits/jetbrains/services/codewhisperer/editor/CodeWhispererEditorUtil.kt b/plugins/amazonq/codewhisperer/jetbrains-community/src/software/aws/toolkits/jetbrains/services/codewhisperer/editor/CodeWhispererEditorUtil.kt index 874a7e25861..61b79766881 100644 --- a/plugins/amazonq/codewhisperer/jetbrains-community/src/software/aws/toolkits/jetbrains/services/codewhisperer/editor/CodeWhispererEditorUtil.kt +++ b/plugins/amazonq/codewhisperer/jetbrains-community/src/software/aws/toolkits/jetbrains/services/codewhisperer/editor/CodeWhispererEditorUtil.kt @@ -32,7 +32,8 @@ object CodeWhispererEditorUtil { val fileName = getFileName(psiFile) val programmingLanguage = psiFile.programmingLanguage() val fileRelativePath = getRelativePathToContentRoot(editor) - return FileContextInfo(caretContext, fileName, programmingLanguage, fileRelativePath) + val fileUri = getFileUri(psiFile) + return FileContextInfo(caretContext, fileName, programmingLanguage, fileRelativePath, fileUri) } fun extractCaretContext(editor: Editor): CaretContext { @@ -73,6 +74,11 @@ object CodeWhispererEditorUtil { private fun getFileName(psiFile: PsiFile): String = psiFile.name.substring(0, psiFile.name.length.coerceAtMost(CodeWhispererConstants.FILENAME_CHARS_LIMIT)) + private fun getFileUri(psiFile: PsiFile): String? = + psiFile.virtualFile?.takeIf { it.isValid }?.let { vFile -> + vFile.url.substring(0, vFile.url.length.coerceAtMost(CodeWhispererConstants.FILENAME_CHARS_LIMIT)) + } + fun getRelativePathToContentRoot(editor: Editor): String? = editor.project?.let { project -> FileDocumentManager.getInstance().getFile(editor.document)?.let { vFile -> diff --git a/plugins/amazonq/codewhisperer/jetbrains-community/src/software/aws/toolkits/jetbrains/services/codewhisperer/model/CodeWhispererModel.kt b/plugins/amazonq/codewhisperer/jetbrains-community/src/software/aws/toolkits/jetbrains/services/codewhisperer/model/CodeWhispererModel.kt index af2bd28c33e..fdf4c4e7377 100644 --- a/plugins/amazonq/codewhisperer/jetbrains-community/src/software/aws/toolkits/jetbrains/services/codewhisperer/model/CodeWhispererModel.kt +++ b/plugins/amazonq/codewhisperer/jetbrains-community/src/software/aws/toolkits/jetbrains/services/codewhisperer/model/CodeWhispererModel.kt @@ -59,6 +59,7 @@ data class FileContextInfo( val filename: String, val programmingLanguage: CodeWhispererProgrammingLanguage, val fileRelativePath: String?, + val fileUri: String?, ) data class SupplementalContextInfo( diff --git a/plugins/amazonq/codewhisperer/jetbrains-community/src/software/aws/toolkits/jetbrains/services/codewhisperer/service/CodeWhispererService.kt b/plugins/amazonq/codewhisperer/jetbrains-community/src/software/aws/toolkits/jetbrains/services/codewhisperer/service/CodeWhispererService.kt index 5a548b23895..67ff8112772 100644 --- a/plugins/amazonq/codewhisperer/jetbrains-community/src/software/aws/toolkits/jetbrains/services/codewhisperer/service/CodeWhispererService.kt +++ b/plugins/amazonq/codewhisperer/jetbrains-community/src/software/aws/toolkits/jetbrains/services/codewhisperer/service/CodeWhispererService.kt @@ -860,6 +860,7 @@ class CodeWhispererService(private val cs: CoroutineScope) : Disposable { .leftFileContent(fileContextInfo.caretContext.leftFileContext) .rightFileContent(fileContextInfo.caretContext.rightFileContext) .filename(fileContextInfo.fileRelativePath ?: fileContextInfo.filename) + .fileUri(fileContextInfo.fileUri) .programmingLanguage(programmingLanguage) .build() val supplementalContexts = supplementalContext?.contents?.map { diff --git a/plugins/amazonq/codewhisperer/jetbrains-community/src/software/aws/toolkits/jetbrains/services/codewhisperer/service/CodeWhispererServiceNew.kt b/plugins/amazonq/codewhisperer/jetbrains-community/src/software/aws/toolkits/jetbrains/services/codewhisperer/service/CodeWhispererServiceNew.kt index 633a20ac0f9..15003451181 100644 --- a/plugins/amazonq/codewhisperer/jetbrains-community/src/software/aws/toolkits/jetbrains/services/codewhisperer/service/CodeWhispererServiceNew.kt +++ b/plugins/amazonq/codewhisperer/jetbrains-community/src/software/aws/toolkits/jetbrains/services/codewhisperer/service/CodeWhispererServiceNew.kt @@ -838,6 +838,7 @@ class CodeWhispererServiceNew(private val cs: CoroutineScope) : Disposable { .leftFileContent(fileContextInfo.caretContext.leftFileContext) .rightFileContent(fileContextInfo.caretContext.rightFileContext) .filename(fileContextInfo.fileRelativePath ?: fileContextInfo.filename) + .fileUri(fileContextInfo.fileUri) .programmingLanguage(programmingLanguage) .build() val supplementalContexts = supplementalContext?.contents?.map { diff --git a/plugins/amazonq/codewhisperer/jetbrains-community/tst/software/aws/toolkits/jetbrains/services/codewhisperer/CodeWhispererCodeCoverageTrackerTest.kt b/plugins/amazonq/codewhisperer/jetbrains-community/tst/software/aws/toolkits/jetbrains/services/codewhisperer/CodeWhispererCodeCoverageTrackerTest.kt index 057ccfca31a..d181f8053b5 100644 --- a/plugins/amazonq/codewhisperer/jetbrains-community/tst/software/aws/toolkits/jetbrains/services/codewhisperer/CodeWhispererCodeCoverageTrackerTest.kt +++ b/plugins/amazonq/codewhisperer/jetbrains-community/tst/software/aws/toolkits/jetbrains/services/codewhisperer/CodeWhispererCodeCoverageTrackerTest.kt @@ -162,7 +162,7 @@ internal class CodeWhispererCodeCoverageTrackerTestPython : CodeWhispererCodeCov fixture.editor, mock(), mock(), - FileContextInfo(mock(), pythonFileName, CodeWhispererPython.INSTANCE, pythonFileName), + FileContextInfo(mock(), pythonFileName, CodeWhispererPython.INSTANCE, pythonFileName, null), runBlocking { async { SupplementalContextInfo( diff --git a/plugins/amazonq/codewhisperer/jetbrains-community/tst/software/aws/toolkits/jetbrains/services/codewhisperer/CodeWhispererFileContextProviderTest.kt b/plugins/amazonq/codewhisperer/jetbrains-community/tst/software/aws/toolkits/jetbrains/services/codewhisperer/CodeWhispererFileContextProviderTest.kt index 3cdc74dbcfb..588aa7c844b 100644 --- a/plugins/amazonq/codewhisperer/jetbrains-community/tst/software/aws/toolkits/jetbrains/services/codewhisperer/CodeWhispererFileContextProviderTest.kt +++ b/plugins/amazonq/codewhisperer/jetbrains-community/tst/software/aws/toolkits/jetbrains/services/codewhisperer/CodeWhispererFileContextProviderTest.kt @@ -109,6 +109,7 @@ class CodeWhispererFileContextProviderTest { ), "Foo.java", CodeWhispererJava.INSTANCE, + "", "" ) @@ -128,6 +129,7 @@ class CodeWhispererFileContextProviderTest { ), "Foo.java", CodeWhispererJava.INSTANCE, + "", "" ) diff --git a/plugins/amazonq/codewhisperer/jetbrains-community/tst/software/aws/toolkits/jetbrains/services/codewhisperer/CodeWhispererServiceTest.kt b/plugins/amazonq/codewhisperer/jetbrains-community/tst/software/aws/toolkits/jetbrains/services/codewhisperer/CodeWhispererServiceTest.kt index b8d6322500c..46dd2551f15 100644 --- a/plugins/amazonq/codewhisperer/jetbrains-community/tst/software/aws/toolkits/jetbrains/services/codewhisperer/CodeWhispererServiceTest.kt +++ b/plugins/amazonq/codewhisperer/jetbrains-community/tst/software/aws/toolkits/jetbrains/services/codewhisperer/CodeWhispererServiceTest.kt @@ -117,7 +117,8 @@ class CodeWhispererServiceTest { CaretContext(leftFileContext = "", rightFileContext = "public class Main {}", leftContextOnCurrentLine = ""), "main.java", CodeWhispererJava.INSTANCE, - "main.java" + "main.java", + "temp:///src/main.java" ) ) } @@ -240,6 +241,7 @@ private fun CodeWhispererProgrammingLanguage.toSdkModel(): ProgrammingLanguage = private fun FileContextInfo.toSdkModel(): FileContext = FileContext.builder() .filename(fileRelativePath) + .fileUri(fileUri) .programmingLanguage(programmingLanguage.toCodeWhispererRuntimeLanguage().toSdkModel()) .leftFileContent(caretContext.leftFileContext) .rightFileContent(caretContext.rightFileContext) diff --git a/plugins/amazonq/codewhisperer/jetbrains-community/tstFixtures/software/aws/toolkits/jetbrains/services/codewhisperer/CodeWhispererTestUtil.kt b/plugins/amazonq/codewhisperer/jetbrains-community/tstFixtures/software/aws/toolkits/jetbrains/services/codewhisperer/CodeWhispererTestUtil.kt index d9474dde6dc..bd40ed11397 100644 --- a/plugins/amazonq/codewhisperer/jetbrains-community/tstFixtures/software/aws/toolkits/jetbrains/services/codewhisperer/CodeWhispererTestUtil.kt +++ b/plugins/amazonq/codewhisperer/jetbrains-community/tstFixtures/software/aws/toolkits/jetbrains/services/codewhisperer/CodeWhispererTestUtil.kt @@ -397,12 +397,13 @@ fun aFileContextInfo(language: CodeWhispererProgrammingLanguage? = null): FileCo val caretContextInfo = CaretContext(aString(), aString(), aString()) val fileName = aString() val fileRelativePath = Paths.get("test", fileName).toString() + val fileUri = "file:///$fileRelativePath" val programmingLanguage = language ?: listOf( CodeWhispererPython.INSTANCE, CodeWhispererJava.INSTANCE ).random() - return FileContextInfo(caretContextInfo, fileName, programmingLanguage, fileRelativePath) + return FileContextInfo(caretContextInfo, fileName, programmingLanguage, fileRelativePath, fileUri) } fun aTriggerType(): CodewhispererTriggerType = diff --git a/plugins/core/sdk-codegen/codegen-resources/codewhispererruntime/service-2.json b/plugins/core/sdk-codegen/codegen-resources/codewhispererruntime/service-2.json index 16805ea2b68..d75fdc527bd 100644 --- a/plugins/core/sdk-codegen/codegen-resources/codewhispererruntime/service-2.json +++ b/plugins/core/sdk-codegen/codegen-resources/codewhispererruntime/service-2.json @@ -1596,9 +1596,16 @@ "leftFileContent":{"shape":"FileContextLeftFileContentString"}, "rightFileContent":{"shape":"FileContextRightFileContentString"}, "filename":{"shape":"FileContextFilenameString"}, + "fileUri": {"shape":"FileContextFileUriString"}, "programmingLanguage":{"shape":"ProgrammingLanguage"} } }, + "FileContextFileUriString": { + "type": "string", + "max": 1024, + "min": 1, + "sensitive": true + }, "FileContextFilenameString":{ "type":"string", "max":1024,