diff --git a/core/autocomplete/postprocessing.ts b/core/autocomplete/postprocessing.ts index e2c6f4d33..724c826f2 100644 --- a/core/autocomplete/postprocessing.ts +++ b/core/autocomplete/postprocessing.ts @@ -1,4 +1,5 @@ import type { ILLM } from ".."; +import { longestCommonSubsequence } from "../util/lcs"; import { lineIsRepeated } from "./lineStream"; function rewritesLineAbove(completion: string, prefix: string): boolean { @@ -19,6 +20,29 @@ function rewritesLineAbove(completion: string, prefix: string): boolean { return lineIsRepeated(lineAbove, firstLineOfCompletion); } +const MAX_REPETITION_FREQ_TO_CHECK = 3; +function isExtremeRepetition(completion: string): boolean { + const lines = completion.split("\n"); + if (lines.length < 6) { + return false; + } + for (let freq = 1; freq < MAX_REPETITION_FREQ_TO_CHECK; freq++) { + const lcs = longestCommonSubsequence(lines[0], lines[freq]); + if (lcs.length > 5 || lcs.length > lines[0].length * 0.5) { + let matchCount = 0; + for (let i = 0; i < lines.length; i += freq) { + if (lines[i].includes(lcs)) { + matchCount++; + } + } + if (matchCount * freq > 8 || (matchCount * freq) / lines.length > 0.8) { + return true; + } + } + } + return false; +} + export function postprocessCompletion({ completion, llm, @@ -40,6 +64,11 @@ export function postprocessCompletion({ return undefined; } + // Filter out repetitions of many lines in a row + if (isExtremeRepetition(completion)) { + return undefined; + } + // Remove trailing whitespace completion = completion.trimEnd(); diff --git a/extensions/vscode/src/util/lcs.ts b/core/util/lcs.ts similarity index 100% rename from extensions/vscode/src/util/lcs.ts rename to core/util/lcs.ts diff --git a/extensions/vscode/src/terminal/terminalEmulator.ts b/extensions/vscode/src/terminal/terminalEmulator.ts index adf1a3d33..d76823da6 100644 --- a/extensions/vscode/src/terminal/terminalEmulator.ts +++ b/extensions/vscode/src/terminal/terminalEmulator.ts @@ -1,9 +1,9 @@ /* Terminal emulator - commented because node-pty is causing problems. */ +import { longestCommonSubsequence } from "core/util/lcs"; import * as os from "node:os"; import stripAnsi from "strip-ansi"; import * as vscode from "vscode"; -import { longestCommonSubsequence } from "../util/lcs"; function loadNativeModule(id: string): T | null { try {