Merge pull request #570 from devchat-ai/bug/code-completion-exceeds-limit-#403

Fix: Limit code completion context to prevent exceeding limit
This commit is contained in:
boob.yang 2024-06-20 16:31:14 +08:00 committed by GitHub
commit 01a1c5c491
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -49,7 +49,25 @@ export async function currentFileContext(
} }
const functionRanges = await findFunctionRanges(filepath, ast.rootNode); const functionRanges = await findFunctionRanges(filepath, ast.rootNode);
return await collapseCodeBlock(functionRanges, filepath, contents, curRow, curColumn); const result: {prefix: string, suffix: string} = await collapseCodeBlock(functionRanges, filepath, contents, curRow, curColumn);
const completeContextLimit = DevChatConfig.getInstance().get("complete_context_limit", 3000);
const prefixMaxLength = Math.min(completeContextLimit * 0.7, result.prefix.length);
const suffixMaxLength = completeContextLimit - prefixMaxLength;
let prefix = result.prefix;
let suffix = result.suffix;
if (prefix.length + suffix.length > completeContextLimit) {
if (prefix.length > prefixMaxLength) {
prefix = prefix.slice(-prefixMaxLength);
}
if (suffix.length > suffixMaxLength) {
suffix = suffix.slice(0, suffixMaxLength);
}
}
return { prefix, suffix };
} }