diff --git a/core/autocomplete/filtering/streamTransforms/lineStream.ts b/core/autocomplete/filtering/streamTransforms/lineStream.ts index 9cbf190ec..236f0e3bd 100644 --- a/core/autocomplete/filtering/streamTransforms/lineStream.ts +++ b/core/autocomplete/filtering/streamTransforms/lineStream.ts @@ -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; } } diff --git a/core/autocomplete/filtering/test/testCases.ts b/core/autocomplete/filtering/test/testCases.ts index 33924b1b3..662bf5201 100644 --- a/core/autocomplete/filtering/test/testCases.ts +++ b/core/autocomplete/filtering/test/testCases.ts @@ -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", diff --git a/core/autocomplete/postprocessing/index.ts b/core/autocomplete/postprocessing/index.ts index 1eb3b37da..ed7c41f05 100644 --- a/core/autocomplete/postprocessing/index.ts +++ b/core/autocomplete/postprocessing/index.ts @@ -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); }