continue-custom/core/util/verticalEdit.ts
Nate Sesti 53b61288ec
🎨 Refactor core (#1281)
* 🎨 refactor out react context

* 🎨 refactor local storage

* 🎨 refactor IdeMessenger

* 🔥 removing unused files

* 🚚 move scripts

* ✅ setup VSCode integration testing

* 🎨 fix test-related stuff

* 🚧 testing

* 🎨 tweak terminal cmd+L

* 🧑‍💻 install biome in core

* 🎨 run biome check on core

* 🎨 run biome check in vscode

* 🎨 run biome check in binary

* 🧑‍💻 global biome config

* 🎨 run biome check on core

* 🎨 run biome check in vscode

* 🎨 fix a few biome warnings

* 🚧 WIP on protocols

* 🚧 progress, organizing, about to remove webviewCore protocol

* 🚧 now running

* 🚧 move indexing code to core

* 🚧 WIP on JetBrains indexing implementation

* 🎨 finish listDir

* 🚧 gui fixes

* 🏷️ update IMessenger.on return type

* 🐛 fix jetbrains types + IDE detection

* 🧑‍💻 set up debugging for binary with TcpMessenger

* 👷 fix prepackage.js

* 🧑‍💻 turn off debug mode for intellij

* 🐛 merge fixes

* 🐛 fixes after refactor

* 🐛 merge fixes

* 💄 increase font size in JB

* 🔀 merge changes

* 🩹 small fixes

* 🐛 fix jetbrains build

* 🩹 more jetbrains fixes

* 🎨 jetbrains improvements

* 🐛 fix lancedb prob in jetbrains build

* 👷 update jetbrains build process

* ✨ intellij problems context provider

* 👷 add script utils file

* 💚 fix jetbrains build regression

* 🎨 dynamic import transformers.js

* 🩹 small jetbrains updates

* ✨ folder context provider in jetbrains

* 🎨 many more jetbrains improvements

* 🔀 merge fixes

* ⚡️ small improvements

* 🎨 tell users transformers.js not supported

* 🎨 trial updates

* 📝 update jetbrains readmej

* ⚡️ only use smaller context for local autocomplete models

* ⚡️ improve bracket filter

* ✨ global .continue/.prompts folder

* 💄 improved model setup process

* 🥅 display VSCode req errs
2024-05-24 19:38:13 -07:00

132 lines
3.5 KiB
TypeScript

import {
filterCodeBlockLines,
filterEnglishLinesAtEnd,
filterEnglishLinesAtStart,
filterLeadingAndTrailingNewLineInsertion,
skipLines,
stopAtLines,
} from "../autocomplete/lineStream.js";
import { streamDiff } from "../diff/streamDiff.js";
import { streamLines } from "../diff/util.js";
import {
ChatMessage,
DiffLine,
ILLM,
LLMFullCompletionOptions,
ModelProvider,
} from "../index.js";
import { gptEditPrompt } from "../llm/templates/edit.js";
import { Telemetry } from "./posthog.js";
function constructPrompt(
prefix: string,
highlighted: string,
suffix: string,
llm: ILLM,
userInput: string,
language: string | undefined,
): string | ChatMessage[] {
const template = llm.promptTemplates?.edit ?? gptEditPrompt;
return llm.renderPromptTemplate(template, [], {
userInput,
prefix,
codeToEdit: highlighted,
suffix,
language: language ?? "",
});
}
async function* addIndentation(
diffLineGenerator: AsyncGenerator<DiffLine>,
indentation: string,
): AsyncGenerator<DiffLine> {
for await (const diffLine of diffLineGenerator) {
yield {
...diffLine,
line: indentation + diffLine.line,
};
}
}
function modelIsInept(model: string): boolean {
return !(model.includes("gpt") || model.includes("claude"));
}
function isGpt4Trial(model: string, provider: ModelProvider): boolean {
return provider === "free-trial" && model.startsWith("gpt-4");
}
export async function* streamDiffLines(
prefix: string,
highlighted: string,
suffix: string,
llm: ILLM,
input: string,
language: string | undefined,
onlyOneInsertion?: boolean,
): AsyncGenerator<DiffLine> {
Telemetry.capture("inlineEdit", {
model: llm.model,
provider: llm.providerName,
});
// Strip common indentation for the LLM, then add back after generation
let oldLines =
highlighted.length > 0
? highlighted.split("\n")
: // When highlighted is empty, we need to combine last line of prefix and first line of suffix to determine the line being edited
[(prefix + suffix).split("\n")[prefix.split("\n").length - 1]];
// But if that line is empty, we can assume we are insertion-only
if (oldLines.length === 1 && oldLines[0].trim() === "") {
oldLines = [];
}
const prompt = constructPrompt(
prefix,
highlighted,
suffix,
llm,
input,
language,
);
const inept = modelIsInept(llm.model);
const options: LLMFullCompletionOptions = {};
if (isGpt4Trial(llm.model, llm.providerName)) {
options.maxTokens = 2048;
}
const completion =
typeof prompt === "string"
? llm.streamComplete(prompt, { raw: true })
: llm.streamChat(prompt);
let lines = streamLines(completion);
lines = filterEnglishLinesAtStart(lines);
lines = filterCodeBlockLines(lines);
lines = stopAtLines(lines);
lines = skipLines(lines);
if (inept) {
// lines = fixCodeLlamaFirstLineIndentation(lines);
lines = filterEnglishLinesAtEnd(lines);
}
let diffLines = streamDiff(oldLines, lines);
diffLines = filterLeadingAndTrailingNewLineInsertion(diffLines);
if (highlighted.length === 0) {
const line = prefix.split("\n").slice(-1)[0];
const indentation = line.slice(0, line.length - line.trimStart().length);
diffLines = addIndentation(diffLines, indentation);
}
let seenGreen = false;
for await (const diffLine of diffLines) {
yield diffLine;
if (diffLine.type === "new") {
seenGreen = true;
} else if (onlyOneInsertion && seenGreen && diffLine.type === "same") {
break;
}
}
}