Skip to content

Commit b150cb8

Browse files
committed
projectContextController & abtest
1 parent fbdd4d3 commit b150cb8

File tree

4 files changed

+58
-6
lines changed

4 files changed

+58
-6
lines changed

plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/cwc/editor/context/project/ProjectContextController.kt

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ import com.intellij.openapi.components.Service
88
import com.intellij.openapi.components.service
99
import com.intellij.openapi.project.Project
1010
import com.intellij.openapi.util.Disposer
11+
import com.intellij.openapi.vfs.VirtualFileManager
12+
import com.intellij.openapi.vfs.newvfs.BulkFileListener
13+
import com.intellij.openapi.vfs.newvfs.events.VFileCreateEvent
14+
import com.intellij.openapi.vfs.newvfs.events.VFileDeleteEvent
15+
import com.intellij.openapi.vfs.newvfs.events.VFileEvent
1116
import kotlinx.coroutines.CoroutineScope
1217
import kotlinx.coroutines.launch
1318
import software.aws.toolkits.core.utils.getLogger
@@ -18,14 +23,29 @@ import software.aws.toolkits.jetbrains.services.codewhisperer.settings.CodeWhisp
1823
class ProjectContextController(private val project: Project, private val cs: CoroutineScope) : Disposable {
1924
private val encoderServer: EncoderServer = EncoderServer(project)
2025
private val projectContextProvider: ProjectContextProvider = ProjectContextProvider(project, encoderServer, cs)
26+
2127
init {
2228
cs.launch {
2329
if (CodeWhispererSettings.getInstance().isProjectContextEnabled()) {
2430
encoderServer.downloadArtifactsAndStartServer()
2531
}
2632
}
33+
34+
project.messageBus.connect(this).subscribe(
35+
VirtualFileManager.VFS_CHANGES,
36+
object : BulkFileListener {
37+
override fun after(events: MutableList<out VFileEvent>) {
38+
val createdFiles = events.filterIsInstance<VFileCreateEvent>().mapNotNull { it.file?.path }
39+
val deletedFiles = events.filterIsInstance<VFileDeleteEvent>().map { it.file.path }
40+
41+
updateIndex(createdFiles, ProjectContextProvider.IndexUpdateMode.ADD)
42+
updateIndex(deletedFiles, ProjectContextProvider.IndexUpdateMode.REMOVE)
43+
}
44+
}
45+
)
2746
}
2847

48+
2949
fun getProjectContextIndexComplete() = projectContextProvider.isIndexComplete.get()
3050

3151
fun query(prompt: String): List<RelevantDocument> {
@@ -37,9 +57,22 @@ class ProjectContextController(private val project: Project, private val cs: Cor
3757
}
3858
}
3959

60+
fun queryInline(query: String, filePath: String): List<InlineBm25Chunk> {
61+
try {
62+
return projectContextProvider.queryInline(query, filePath)
63+
} catch (e: Exception) {
64+
logger.warn { "error while querying inline for project context $e.message" }
65+
return emptyList()
66+
}
67+
}
68+
4069
fun updateIndex(filePath: String) {
70+
updateIndex(listOf(filePath), ProjectContextProvider.IndexUpdateMode.UPDATE)
71+
}
72+
73+
fun updateIndex(filePaths: List<String>, mode: ProjectContextProvider.IndexUpdateMode) {
4174
try {
42-
return projectContextProvider.updateIndex(filePath)
75+
return projectContextProvider.updateIndex(filePaths, mode)
4376
} catch (e: Exception) {
4477
logger.warn { "error while updating index for project context $e.message" }
4578
}

plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/cwc/editor/context/project/ProjectContextEditorListener.kt

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,22 @@
22
// SPDX-License-Identifier: Apache-2.0
33

44
package software.aws.toolkits.jetbrains.services.cwc.editor.context.project
5+
import com.intellij.openapi.fileEditor.FileDocumentManager
56
import com.intellij.openapi.fileEditor.FileEditorManager
7+
import com.intellij.openapi.fileEditor.FileEditorManagerEvent
68
import com.intellij.openapi.fileEditor.FileEditorManagerListener
79
import com.intellij.openapi.vfs.VirtualFile
810
import software.aws.toolkits.jetbrains.services.codewhisperer.settings.CodeWhispererSettings
911

1012
class ProjectContextEditorListener : FileEditorManagerListener {
11-
override fun fileClosed(source: FileEditorManager, file: VirtualFile) {
12-
if (CodeWhispererSettings.getInstance().isProjectContextEnabled()) {
13-
ProjectContextController.getInstance(source.project).updateIndex(file.path)
13+
override fun selectionChanged(event: FileEditorManagerEvent) {
14+
val oldFile = event.oldFile ?: return
15+
16+
// TODO: should respect isIdeAutosave config
17+
with(FileDocumentManager.getInstance()) {
18+
getDocument(oldFile)?.let {
19+
saveDocument(it)
20+
}
1421
}
1522
}
1623
}

plugins/amazonq/chat/jetbrains-community/src/software/aws/toolkits/jetbrains/services/cwc/editor/context/project/ProjectContextProvider.kt

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@ class ProjectContextProvider(val project: Project, private val encoderServer: En
5454
}
5555
}
5656

57+
enum class IndexUpdateMode(val value: String) {
58+
UPDATE("update"),
59+
REMOVE("remove"),
60+
ADD("add"),
61+
}
62+
5763
data class FileCollectionResult(
5864
val files: List<String>,
5965
val fileSize: Int,
@@ -185,9 +191,9 @@ class ProjectContextProvider(val project: Project, private val encoderServer: En
185191
}
186192
}
187193

188-
fun updateIndex(filePaths: List<String>, mode: String) {
194+
fun updateIndex(filePaths: List<String>, mode: IndexUpdateMode) {
189195
if (!isIndexComplete.get()) return
190-
val encrypted = encryptRequest(UpdateIndexRequest(filePaths, mode))
196+
val encrypted = encryptRequest(UpdateIndexRequest(filePaths, mode.value))
191197
sendMsgToLsp(LspMessage.UpdateIndex, encrypted)
192198
}
193199

plugins/amazonq/codewhisperer/jetbrains-community/src/software/aws/toolkits/jetbrains/services/codewhisperer/service/CodeWhispererFeatureConfigService.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import com.intellij.util.concurrency.annotations.RequiresBackgroundThread
1010
import software.amazon.awssdk.services.codewhispererruntime.model.FeatureValue
1111
import software.aws.toolkits.core.utils.debug
1212
import software.aws.toolkits.core.utils.getLogger
13+
import software.aws.toolkits.jetbrains.isDeveloperMode
1314
import software.aws.toolkits.jetbrains.services.codewhisperer.credentials.CodeWhispererClientAdaptor
1415
import software.aws.toolkits.jetbrains.services.codewhisperer.util.calculateIfBIDConnection
1516
import software.aws.toolkits.jetbrains.services.codewhisperer.util.calculateIfIamIdentityCenterConnection
@@ -94,6 +95,9 @@ class CodeWhispererFeatureConfigService {
9495

9596
fun getNewAutoTriggerUX(): Boolean = getFeatureValueForKey(NEW_AUTO_TRIGGER_UX).boolValue()
9697

98+
// TODO: remove dev mode flag
99+
fun getInlineCompletion(): Boolean = if(isDeveloperMode()) true else getFeatureValueForKey(INLINE_COMPLETION).boolValue()
100+
97101
// Get the feature value for the given key.
98102
// In case of a misconfiguration, it will return a default feature value of Boolean false.
99103
private fun getFeatureValueForKey(name: String): FeatureValue =
@@ -103,6 +107,8 @@ class CodeWhispererFeatureConfigService {
103107
companion object {
104108
fun getInstance(): CodeWhispererFeatureConfigService = service()
105109
private const val TEST_FEATURE_NAME = "testFeature"
110+
// TODO: update
111+
private const val INLINE_COMPLETION = "inlineCompletion"
106112
private const val DATA_COLLECTION_FEATURE = "IDEProjectContextDataCollection"
107113
const val CUSTOMIZATION_ARN_OVERRIDE_NAME = "customizationArnOverride"
108114
private const val NEW_AUTO_TRIGGER_UX = "newAutoTriggerUX"

0 commit comments

Comments
 (0)