Merge pull request #3126 from continuedev/tomasz/improve-autocomplete-timeout

Ensure sure at least one non-whitespace line is returned
This commit is contained in:
Nate Sesti 2024-11-28 12:51:01 -08:00 • committed by GitHub
commit 8f2edef29e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 35 additions and 24 deletions

View file

@ -537,12 +537,17 @@ export async function* showWhateverWeHaveAtXMs(
ms: number,
): LineStream {
const startTime = Date.now();
let firstNonWhitespaceLineYielded = false;
for await (const line of lines) {
// Always get at least one line
yield line;
// But after that, soft break at X ms
if (Date.now() - startTime > ms) {
if (!firstNonWhitespaceLineYielded && line.trim() !== "") {
firstNonWhitespaceLineYielded = true;
}
const isTakingTooLong = Date.now() - startTime > ms;
if (isTakingTooLong && firstNonWhitespaceLineYielded) {
break;
}
}

View file

@ -333,6 +333,21 @@ export const Timer = () => {
llmOutput: ` this;`,
expectedCompletion: `this;`,
},
{
description:
"Should autocomplete SQL query with subquery and alias in SELECT clause",
filename: "complex_query.sql",
input: `SELECT u.id,
u.name,
(SELECT COUNT(*) FROM orders o WHERE o.user_id = u.id) AS order_count
FROM users u
WHERE u.active = 1
<|fim|>`,
llmOutput:
" AND EXISTS (SELECT 1 FROM transactions t WHERE t.user_id = u.id AND t.amount > 100)",
expectedCompletion:
"AND EXISTS (SELECT 1 FROM transactions t WHERE t.user_id = u.id AND t.amount > 100)",
},
];
export const TEST_CASES_WITHOUT_DIFF: AutocompleteFileringTestInput[] = [
@ -832,21 +847,6 @@ func main() {
llmOutput: " DEFAULT",
expectedCompletion: " DEFAULT",
},
{
description:
"Should autocomplete SQL query with subquery and alias in SELECT clause",
filename: "complex_query.sql",
input: `SELECT u.id,
u.name,
(SELECT COUNT(*) FROM orders o WHERE o.user_id = u.id) AS order_count
FROM users u
WHERE u.active = 1
<|fim|>`,
llmOutput:
" AND EXISTS (SELECT 1 FROM transactions t WHERE t.user_id = u.id AND t.amount > 100)",
expectedCompletion:
" AND EXISTS (SELECT 1 FROM transactions t WHERE t.user_id = u.id AND t.amount > 100)",
},
{
description:
"Should autocomplete multi-line SQL query with CASE statements",

View file

@ -43,11 +43,15 @@ function isExtremeRepetition(completion: string): boolean {
}
return false;
}
export function isOnlyWhitespace(completion: string): boolean {
function isOnlyWhitespace(completion: string): boolean {
const whitespaceRegex = /^[\s]+$/;
return whitespaceRegex.test(completion);
}
function isBlank(completion: string): boolean {
return completion.trim().length === 0;
}
export function postprocessCompletion({
completion,
llm,
@ -60,6 +64,11 @@ export function postprocessCompletion({
suffix: string;
}): string | undefined {
// Don't return empty
if (isBlank(completion)) {
return undefined;
}
// Don't return whitespace
if (isOnlyWhitespace(completion)) {
return undefined;
}
@ -112,11 +121,8 @@ export function postprocessCompletion({
// }
// If prefix ends with space and so does completion, then remove the space from completion
if (
prefix.split("\n").pop()?.trim() !== "" &&
prefix.endsWith(" ") &&
completion.startsWith(" ")
) {
if (prefix.endsWith(" ") && completion.startsWith(" ")) {
completion = completion.slice(1);
}