From 4c94fd84464b2bc1b7ff22ba298f256c34ebf62a Mon Sep 17 00:00:00 2001 From: Jacob Kim Date: Mon, 28 Jul 2025 15:42:16 -0700 Subject: [PATCH] refactor: cleanup --- core/nextEdit/NextEditProvider.ts | 14 ++-- .../templating/NextEditPromptEngine.ts | 67 +------------------ 2 files changed, 11 insertions(+), 70 deletions(-) diff --git a/core/nextEdit/NextEditProvider.ts b/core/nextEdit/NextEditProvider.ts index 3af145859..b431f0503 100644 --- a/core/nextEdit/NextEditProvider.ts +++ b/core/nextEdit/NextEditProvider.ts @@ -71,7 +71,6 @@ export class NextEditProvider { private previousRequest: AutocompleteInput | null = null; private previousCompletions: NextEditOutcome[] = []; private nextEditableRegionsInTheCurrentChain: RangeInFile[] = []; - // TODO: keep track of the last completion request (at least the file path it came from) private constructor( private readonly configHandler: ConfigHandler, @@ -305,6 +304,7 @@ export class NextEditProvider { // TODO: Toggle between the default endpoint and the finetuned endpoint. const prompts: Prompt[] = []; + if (this.endpointType === "default") { prompts.push(renderDefaultSystemPrompt()); prompts.push(renderDefaultUserPrompt(snippetPayload, helper)); @@ -319,11 +319,12 @@ export class NextEditProvider { diffType: DiffFormatType.Unified, contextLines: 3, }); + const promptMetadata = await renderPrompt( helper, historyDiff ?? this.diffContext, - EditableRegionStrategy.Naive, ); + this.promptMetadata = promptMetadata; prompts.push({ @@ -336,6 +337,7 @@ export class NextEditProvider { "If the user has made a change, make sure you respect that and try to apply it to other parts of the code that used to use the old content.", ].join("\n"), }); + prompts.push(promptMetadata.prompt); } @@ -343,10 +345,12 @@ export class NextEditProvider { helper.pos.line - NEXT_EDIT_EDITABLE_REGION_TOP_MARGIN, 0, ); + const editableRegionEndLine = Math.min( helper.pos.line + NEXT_EDIT_EDITABLE_REGION_BOTTOM_MARGIN, helper.fileLines.length - 1, ); + const oldEditRangeSlice = helper.fileContents .split("\n") .slice(editableRegionStartLine, editableRegionEndLine + 1) @@ -361,6 +365,7 @@ export class NextEditProvider { if (this.endpointType === "default") { const msg: ChatMessage = await llm.chat(prompts, token); + if (typeof msg.content === "string") { const nextCompletion = JSON.parse(msg.content).newCode; const outcomeNext: NextEditOutcome = { @@ -368,7 +373,6 @@ export class NextEditProvider { modelProvider: llm.underlyingProviderName, modelName: llm.model + ":zetaDataset", completionOptions: null, - // filepath: helper.filepath, completionId: helper.input.completionId, gitRepo: await this.ide.getRepoName(helper.filepath), uniqueId: await this.ide.getUniqueId(), @@ -413,14 +417,17 @@ export class NextEditProvider { : ""; const currCursorPos = helper.pos; + const editableRegionStartLine = Math.max( currCursorPos.line - NEXT_EDIT_EDITABLE_REGION_TOP_MARGIN, 0, ); + const editableRegionEndLine = Math.min( currCursorPos.line + NEXT_EDIT_EDITABLE_REGION_BOTTOM_MARGIN, helper.fileLines.length - 1, ); + const oldEditRangeSlice = helper.fileContents .split("\n") .slice(editableRegionStartLine, editableRegionEndLine + 1) @@ -438,7 +445,6 @@ export class NextEditProvider { modelProvider: llm.underlyingProviderName, modelName: llm.model + ":zetaDataset", completionOptions: null, - // filepath: helper.filepath, completionId: helper.input.completionId, gitRepo: await this.ide.getRepoName(helper.filepath), uniqueId: await this.ide.getUniqueId(), diff --git a/core/nextEdit/templating/NextEditPromptEngine.ts b/core/nextEdit/templating/NextEditPromptEngine.ts index d6cd8dacf..bad3f5166 100644 --- a/core/nextEdit/templating/NextEditPromptEngine.ts +++ b/core/nextEdit/templating/NextEditPromptEngine.ts @@ -1,5 +1,5 @@ import Handlebars from "handlebars"; -import { Position, RangeInFile } from "../.."; +import { Position } from "../.."; import { SnippetPayload } from "../../autocomplete/snippets"; import { HelperVars } from "../../autocomplete/util/HelperVars"; import { @@ -9,9 +9,7 @@ import { NEXT_EDIT_EDITABLE_REGION_TOP_MARGIN, USER_CURSOR_IS_HERE_TOKEN, } from "../constants"; -import { EditableRegionStrategy } from "../NextEditEditableRegionCalculator"; import { - NextEditOutcome, NextEditTemplate, PromptMetadata, SystemPrompt, @@ -51,9 +49,6 @@ function templateRendererOfModel( export async function renderPrompt( helper: HelperVars, userEdits: string, - editableRegionStrategy: EditableRegionStrategy, - prevOutcome?: NextEditOutcome, - toApply?: RangeInFile, ): Promise { let modelName = helper.modelName as NextEditModelName; @@ -87,70 +82,10 @@ export async function renderPrompt( const renderer = templateRendererOfModel(modelName); let editedCodeWithTokens = ""; - // TODO: apply prevOutcome to existing content. we will need the line range. - // Also display the diff between existing content and applied content. - // NOTE: This only runs in fetchFunction. - // if (prevOutcome) { - // // console.log("renderPrompt prevOutcome:", prevOutcome.completion); - // // console.log( - // // "beforeInsert:", - // // helper.fileLines.slice(0, prevOutcome?.editableRegionStartLine), - // // ); - // // console.log("toInsert: ", prevOutcome?.completion.split("\n")); - // // console.log( - // // "afterInsert:", - // // helper.fileLines.slice(prevOutcome?.editableRegionEndLine), - // // ); - // let appliedContent = [ - // ...helper.fileLines.slice(0, prevOutcome?.editableRegionStartLine), - // ...(prevOutcome?.completion.split("\n") ?? []), - // ...helper.fileLines.slice(prevOutcome?.editableRegionEndLine), - // ]; - // // console.log("helper.fileContents:", helper.fileContents); - // // NOTE: I'm not sure, but <|user_cursor_is_here|> is somehow being added to appliedContent. - // // The individual parts don't contain this. - // // console.log("appliedContent:", appliedContent); - // appliedContent = appliedContent.filter( - // (line) => !line.includes(USER_CURSOR_IS_HERE_TOKEN), - // ); - // // Given a previous response, we need to calculate where the cursor would be. - // // I think it's a good idea to calculate this in the response. - - // // TODO: handle cases where there are more than one next editable region. - // // NOTE: we might set it so that we receive the next editable region as an argument. - // const editableRegion = await getNextEditableRegion(editableRegionStrategy, { - // fileLines: appliedContent, - // filepath: helper.filepath, - // }); - - // // console.log("appliedContent:", appliedContent); - // // console.log("startLine:", editableRegion?.range.start.line); - // editedCodeWithTokens = insertTokens( - // appliedContent, - // prevOutcome.finalCursorPosition, - // editableRegion && editableRegion[0]?.range.start.line !== undefined - // ? editableRegion[0].range.start.line - // : undefined, - // editableRegion && editableRegion[0]?.range.end.line !== undefined - // ? editableRegion[0].range.end.line - // : undefined, - // ); - // // console.log("new editedCodeWithTokens:", editedCodeWithTokens); - // userEdits = createDiff({ - // beforeContent: helper.fileContents, - // afterContent: appliedContent.join("\n"), - // filePath: helper.filepath, - // diffType: DiffFormatType.Unified, - // contextLines: 3, - // }); - // userEdits = userEdits.replace(USER_CURSOR_IS_HERE_TOKEN, ""); - // // console.log("new userDiff:", userEdits); - // } else { editedCodeWithTokens = insertTokens( helper.fileContents.split("\n"), helper.pos, ); - // } const tv: TemplateVars = { userEdits,