From 8a4003672b7b9e8b1126bd3aa3e2208a058fcb6b Mon Sep 17 00:00:00 2001 From: Nate Sesti Date: Tue, 11 Jun 2024 15:28:59 -0700 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=EF=B8=8F=20filter=20out=20extreme=20r?= =?UTF-8?q?epetitions=20from=20autocomplete?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/autocomplete/postprocessing.ts | 29 +++++++++++++++++++ {extensions/vscode/src => core}/util/lcs.ts | 0 .../vscode/src/terminal/terminalEmulator.ts | 2 +- 3 files changed, 30 insertions(+), 1 deletion(-) rename {extensions/vscode/src => core}/util/lcs.ts (100%) 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 {