fix: Limit code completion context to prevent exceeding limit

- Adjusted collapseCodeBlock function to respect complete_context_limit
- Implemented prefix and suffix length limiting to prevent exceeding limit
- Ensured code completion does not exceed context limit even with folded functions

Closes #403
This commit is contained in:
bobo.yang 2024-06-20 16:30:14 +08:00
parent 2390e45544
commit b96e476c80

View File

@ -49,7 +49,25 @@ export async function currentFileContext(
}
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 };
}