diff --git a/core/autocomplete/streaming.ts b/core/autocomplete/streaming.ts new file mode 100644 index 000000000..f9abf2109 --- /dev/null +++ b/core/autocomplete/streaming.ts @@ -0,0 +1,16 @@ +import { distance } from "fastest-levenshtein"; +import { LineStream } from "../diff/util"; + +export async function* stopAtSimilarLine( + stream: LineStream, + line: string +): AsyncGenerator { + line = line.trim(); + for await (const nextLine of stream) { + const dist = distance(nextLine.trim(), line); + if (nextLine.length > 4 && line.length > 4 && dist / line.length < 0.1) { + break; + } + yield nextLine; + } +} diff --git a/core/diff/util.ts b/core/diff/util.ts index 80ca4f813..ce9a33d66 100644 --- a/core/diff/util.ts +++ b/core/diff/util.ts @@ -56,7 +56,9 @@ export function matchLine( /** * Convert a stream of arbitrary chunks to a stream of lines */ -export async function* streamLines(streamCompletion: LineStream): LineStream { +export async function* streamLines( + streamCompletion: AsyncGenerator +): LineStream { let buffer = ""; for await (const chunk of streamCompletion) { buffer += chunk; diff --git a/core/package-lock.json b/core/package-lock.json index d33a76ad5..394c301d1 100644 --- a/core/package-lock.json +++ b/core/package-lock.json @@ -17,7 +17,7 @@ "fastest-levenshtein": "^1.0.16", "handlebars": "^4.7.8", "js-tiktoken": "^1.0.8", - "llm-code-highlighter": "^0.0.1", + "llm-code-highlighter": "^0.0.3", "node-fetch": "^3.3.2", "node-html-markdown": "^1.3.0", "openai": "^4.20.1", @@ -4770,9 +4770,9 @@ "dev": true }, "node_modules/llm-code-highlighter": { - "version": "0.0.1", - "resolved": "https://registry.npmjs.org/llm-code-highlighter/-/llm-code-highlighter-0.0.1.tgz", - "integrity": "sha512-26PExkQxeh5EiBNroldpV82pZDrjC6C5v4PLcm5ZpGIKExrHs9Hqw36B+1Fg+dxXGZMnwlCZ+6EbC2FEVocmcw==", + "version": "0.0.3", + "resolved": "https://registry.npmjs.org/llm-code-highlighter/-/llm-code-highlighter-0.0.3.tgz", + "integrity": "sha512-l/R+m6ngXRTYxzoCzqpBk5TiSVpS4Ek9fKx/PIy25HV6XDbCC6iOfVITPPSTGkh7NlI+tN5vWjdqUfN3YW+HwQ==", "dependencies": { "graphology": "^0.25.4", "graphology-metrics": "^2.2.0", diff --git a/core/yarn.lock b/core/yarn.lock index 3c04d91f7..247947ed2 100644 --- a/core/yarn.lock +++ b/core/yarn.lock @@ -2695,10 +2695,10 @@ lines-and-columns@^1.1.6: resolved "https://registry.npmjs.org/lines-and-columns/-/lines-and-columns-1.2.4.tgz" integrity sha512-7ylylesZQ/PV29jhEDl3Ufjo6ZX7gCqJr5F7PKrqc93v7fzSymt1BpwEU8nAUXs8qzzvqhbjhK5QZg6Mt/HkBg== -llm-code-highlighter@^0.0.1: - version "0.0.1" - resolved "https://registry.npmjs.org/llm-code-highlighter/-/llm-code-highlighter-0.0.1.tgz" - integrity sha512-26PExkQxeh5EiBNroldpV82pZDrjC6C5v4PLcm5ZpGIKExrHs9Hqw36B+1Fg+dxXGZMnwlCZ+6EbC2FEVocmcw== +llm-code-highlighter@^0.0.3: + version "0.0.3" + resolved "https://registry.npmjs.org/llm-code-highlighter/-/llm-code-highlighter-0.0.3.tgz" + integrity sha512-l/R+m6ngXRTYxzoCzqpBk5TiSVpS4Ek9fKx/PIy25HV6XDbCC6iOfVITPPSTGkh7NlI+tN5vWjdqUfN3YW+HwQ== dependencies: graphology "^0.25.4" graphology-metrics "^2.2.0" diff --git a/extensions/vscode/src/lang-server/completionProvider.ts b/extensions/vscode/src/lang-server/completionProvider.ts index 95114db98..cf7fdb80c 100644 --- a/extensions/vscode/src/lang-server/completionProvider.ts +++ b/extensions/vscode/src/lang-server/completionProvider.ts @@ -6,7 +6,9 @@ import { languageForFilepath, } from "core/autocomplete/constructPrompt"; import { DEFAULT_AUTOCOMPLETE_OPTS } from "core/autocomplete/parameters"; +import { stopAtSimilarLine } from "core/autocomplete/streaming"; import { getTemplateForModel } from "core/autocomplete/templates"; +import { streamLines } from "core/diff/util"; import Handlebars from "handlebars"; import { v4 as uuidv4 } from "uuid"; import * as vscode from "vscode"; @@ -259,6 +261,7 @@ async function getTabCompletion( new vscode.Position(document.lineCount, Number.MAX_SAFE_INTEGER) ) ); + const lineBelowCursor = document.lineAt(pos.line + 1).text; const clipboardText = await vscode.env.clipboard.readText(); const { prefix, suffix } = await constructAutocompletePrompt( document.fileName, @@ -306,25 +309,25 @@ async function getTabCompletion( ); // LLM - for await (const update of generator) { - completion += update; - if (token.isCancellationRequested) { - stopStatusBarLoading(); - return undefined; - } - - let foundEndLine = false; - for (const end of lang.endOfLine) { - if (completion.includes(end + "\n")) { - // completion = - // completion.slice(0, completion.indexOf(end + "\n")) + end; - // foundEndLine = true; - // break; + let cancelled = false; + const generatorWithCancellation = async function* () { + for await (const update of generator) { + if (token.isCancellationRequested) { + stopStatusBarLoading(); + cancelled = true; + return undefined; } + yield update; } - if (foundEndLine) { - break; - } + }; + const lineGenerator = streamLines(generatorWithCancellation()); + const finalGenerator = stopAtSimilarLine(lineGenerator, lineBelowCursor); + for await (const update of finalGenerator) { + completion += update; + } + + if (cancelled) { + return undefined; } // Don't return empty