Skip to content

Commit ae2dce9

Browse files
committed
patch
1 parent 3e5da46 commit ae2dce9

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

plugins/amazonq/codewhisperer/jetbrains-community/src/software/aws/toolkits/jetbrains/services/codewhisperer/util/CodeWhispererFileContextProvider.kt

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,14 @@ class DefaultCodeWhispererFileContextProvider(private val project: Project) : Fi
262262
val projectContext = contexts.find { it.strategy == CrossFileStrategy.Codemap }
263263
val openTabsContext = contexts.find { it.strategy == CrossFileStrategy.OpenTabsBM25 }
264264

265+
/**
266+
* We're using both codemap and opentabs context
267+
* 1. If both are present, codemap should live in the first of supplemental context list, i.e [codemap, opentabs_0, opentabs_1...] with strategy name codemap
268+
* 2. If only one is present, return the one present with corresponding strategy name, either codemap or opentabs
269+
* 3. If none is present, return empty list with strategy name empty
270+
*
271+
* Service will throw 400 error when context length is greater than 20480, drop the last chunk until the total length fits in the cap
272+
*/
265273
val contextBeforeTruncation = when {
266274
projectContext == null && openTabsContext == null -> SupplementalContextInfo.emptyCrossFileContextInfo(targetContext.filename)
267275

@@ -317,14 +325,13 @@ class DefaultCodeWhispererFileContextProvider(private val project: Project) : Fi
317325
else -> SupplementalContextInfo.emptyCrossFileContextInfo(targetContext.filename)
318326
}
319327

320-
val contextAfterTruncation = if (contextBeforeTruncation.contentLength >= CodeWhispererConstants.CrossFile.MAX_TOTAL_LENGTH) {
321-
contextBeforeTruncation.copy(
322-
contents = contextBeforeTruncation.contents.dropLast(1)
323-
)
324-
} else {
325-
contextBeforeTruncation
328+
var c = contextBeforeTruncation.contents
329+
while (c.sumOf { it.content.length } >= CodeWhispererConstants.CrossFile.MAX_TOTAL_LENGTH) {
330+
c = c.dropLast(1)
326331
}
327332

333+
val contextAfterTruncation = contextBeforeTruncation.copy(contents = c)
334+
328335
return contextAfterTruncation
329336
}
330337

0 commit comments

Comments
 (0)